ASP.NET Core MVC and Web API on the same IIS server

72 views Asked by At

I have two applications:

  1. ASP.NET Core MVC web app
  2. ASP.NET Core Web API

When I run the applications on my computer, everything works and the MVC app can make get/post http requests to my API.

When I publish to the server, my LOCAL MVC app still can make the get/post http requests.

But when the ASP.NET Core MVC app is published to the server, it can make just Get requests, but not Post requests anymore.

Both application are on the same server, under the same domain. Both use http and not https.

Does someone know if I'm missing something?

I tried a lot of things: learnt about CORS origin, transfer to http or https. Change the url to post...

2

There are 2 answers

0
Jalpesh Vadgama On

This is a problem of CORS where the browser does prevent the call of your rest apis when anybody tries it from postman or other URLs. Basically what you can do is create CORS policy and allow them as per your requirement.

Here is how you can do this in your program.cs or startup.cs file

builder.Services.AddCors(options =>
{
      options.AddPolicy(name: MyAllowSpecificOrigins,
                  policy  =>
                  {
                      policy.WithOrigins("http://example.com",
                                          "http://www.contoso.com");
                  });
 });

You can find more information about this at following links

https://www.bytehide.com/blog/cors-aspnet-core

https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-8.0

https://medium.com/@M-B-A-R-K/what-is-cors-in-asp-net-core-85f4903c9d43

1
moshe On

i found my problem.

  1. i'm using a http request under the same origin, so its not about cors.
  2. my program was looking for configuration from the appsettings.production file and not from appsettings.json file. and the database connection string wasn't right. thanks for everyone.