AllowAnyOrigin Cors Not working Axios Vuejs
It turns out the .Net Core Server was not set up right, it wasn't until I was trying to use the browser on my local machine did the CORS
problem crop up.
I'm not sure if the CORS implementation changed and I was not aware, or if I just wasn't doing it right from the start, but I was sure I followed guides.
The first thing I changed was ensuring that the Cors Policy was added before configuring the app to use MVC
.
The second thing I did, and I suspect this is optional, but best practice, I also moved the policy logic to the ConfigureServices
method.
My final code looked below. I'm keeping as much in tact to preserve the order.
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddOptions(); services.AddSwaggerGen(); ///Authentication configuration went here. services.AddSingleton<IConfiguration>(Configuration); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseExceptionHandler("/Home/Error"); app.UseStaticFiles(); app.UseCors("CorsPolicy"); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); app.UseSwagger((httpRequest, swaggerDoc) => { swaggerDoc.Host = httpRequest.Host.Value; }); app.UseSwaggerUi(swaggerUrl: "/{appname}/swagger/v1/swagger.json");
Activating the cors didn't work for me for the PUT and DELETE methods, what I did was add some configuration lines in the web.config file to remove the WebDav module.
I used this inside the existing tag system.webServer tag in the web.config file
<modules runAllManagedModulesForAllRequests="false"> <remove name="WebDAVModule" /></modules>
I found the lines in this site:
Modify the web.confighttps://hovercraft.ie/asp-net-core-web-api-put-delete-methods-not-allowed-405-error/
Hope this helps.