I've created an ASP .Net Core 1.1 Web API. I've had some issues with our IONIC2 app calling the API because of CORS issues. So, on my Startup.cs I added:
public void ConfigureServices(IServiceCollection services) { var corsBuilder = new CorsPolicyBuilder(); corsBuilder.AllowAnyHeader(); corsBuilder.AllowAnyMethod(); corsBuilder.AllowAnyOrigin(); corsBuilder.AllowCredentials(); services.AddCors(options => { options.AddPolicy("SiteCorsPolicy", corsBuilder.Build()); });
and
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseCors("SiteCorsPolicy");
That, as far as I understand, enables CORS globally, for all actions in all controllers. And it does indeed now allow the IONIC2 app access to the Web API. However, these is one action (a create record action i.e. HTTP POST) which gives the following error:
inspections.propworx.co.za/api/inspections:1 POST http://inspections.propworx.co.za/api/inspections 404 (Not Found)
XMLHttpRequest cannot load http://inspections.propworx.co.za/api/inspections. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://192.168.1.119:8100' is therefore not allowed access. The response had HTTP status code 404.
core.es5.js:1084
ERROR -> Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers…}
when the IONIC app calls that particular action in the API. The action is:
[HttpPost] [Authorize] public async Task<IActionResult> PostInspection([FromBody] Inspection inspection) { if (!ModelState.IsValid) return BadRequest(ModelState); _context.Inspection.Add(inspection); await _context.SaveChangesAsync(); return CreatedAtAction("GetInspection", new { id = inspection.Id }, inspection); }
Does that sounds like a CORS issue? But why do all the other actions work? Other GET, POST, PUT and DELETE actions work.... but this particular POST action gives that error... Any ideas? The IONIC developer says the error is on my side (Web API) side, and he is probably right...