I created a new ASP.NET Core Web API project and kept its default settings. It runs on this URL: https://localhost:7254/.
I also created an ASP.NET Core MVC web app as API user to test it and also kept its default settings. It runs on URL https://localhost:7120/
Both projects run on .NET 6.
Based on Microsoft documentation, those URLs have different ports so they are not the same origin.
I wonder why can I call my Web API method and use it, without any specific CORS setting?
Here is my Web API project Program.cs:
using Serilog;
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
path: "..\\logs\\log-.txt",
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information
)
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
And here is a part of my ASP web app HomeController code, that calls Web API method:
public IActionResult Index()
{
List<WeatherForecast> list = new List<WeatherForecast>();
var client = new RestClient("https://localhost:7254");
var request = new RestRequest("WeatherForecast", Method.Get);
RestResponse response = client.Execute(request);
if (response.IsSuccessful && response.Content != null)
{
var data = JsonConvert.DeserializeObject<List<WeatherForecast>>(response.Content);
if (data != null)
list = data;
}
return View(list);
}
