Skip to content

ASP.NET Core 3.0 Web API CORS always returning HTTP 500 #14560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TanvirArjel opened this issue Sep 29, 2019 · 2 comments
Closed

ASP.NET Core 3.0 Web API CORS always returning HTTP 500 #14560

TanvirArjel opened this issue Sep 29, 2019 · 2 comments

Comments

@TanvirArjel
Copy link
Contributor

In my ASP.NET Core 3.0 Web API project I have enabled CORS as follows:

public class Startup
 {
     public Startup(IConfiguration configuration)
     {
         Configuration = configuration;
     }

     public IConfiguration Configuration { get; }

     readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
     // This method gets called by the runtime. Use this method to add services to the container.
     public void ConfigureServices(IServiceCollection services)
     {
         services.RegisterDbContext(Configuration);
         services.AddCors(options =>
         {
             options.AddPolicy(MyAllowSpecificOrigins,
                 builder =>
                 {
                     builder.WithOrigins("https://localhost:44367",
                             "http://www.contoso.com")
                         .AllowAnyHeader()
                         .AllowAnyMethod();
                 });
         });
         services.AddControllers();
         
     }

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
         if (env.IsDevelopment())
         {
             app.UseDeveloperExceptionPage();
         }

         app.UseHttpsRedirection();

         app.UseRouting();
         app.UseCors(MyAllowSpecificOrigins);

         app.UseAuthorization();

         app.UseEndpoints(endpoints =>
         {
             endpoints.MapControllers();
         });
     }
 }

But I am always getting HTTP 500 without hitting controller method.

HTTP 500 if request to a CORS-enabled endpoint is requested without an origin header is a similar issue and it is marked as Closed Done!

Then why I am receiving this same error?

@pranavkm
Copy link
Contributor

Could you look at the server logs and include the stack trace for the error?

@TanvirArjel
Copy link
Contributor Author

@pranavkm Thank you for telling this. I have fixed the issue after inspecting the StackTrace and this was not related to ASP.NET Core 3.0 CORS Settings.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants