Andrew Milici
Andrew Milici

Reputation: 155

CORS Pre-Flight fail only one method

I have an extremely unusual issue that has just popped up. I’m writing an Ionic app with a ASP.NET Web API backend. I tackled the pre-flight CORS issues early on, by adding this to the register method of my WebApiConfig class:

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

When navigating each page, and making various $http.get calls, the pre-flight CORS is working a treat (below is network traffic taken from Chrome Developer Tools)

Example 1

However I have recently created an additional method in WebApi:

    [Authorize]
    [Route("api/GetJobType")]
    public IHttpActionResult GetJobType(int? id)
    {
        var model = new JobsModel();
        return this.Ok(model.GetJobType(id.Value));
    }

But the pre-flight OPTIONS request is returning a 404:

enter image description here

This is the $http.get code:

    var url = ApiEndpoint.url + '/GetJobType/' + jobId;
    $http.get(url).then(function(resp) {
        alert(resp.data);

    }, function(err) {
        alert(err);
    });

Any ideas why the OPTIONS request would fail for only one method, while the rest work perfectly.

Upvotes: 0

Views: 262

Answers (1)

Stephen Brickner
Stephen Brickner

Reputation: 2602

It doesn't appear to have anything to do with the pre-flight request, it's your route. You are getting a 404 which means it cannot find the requested resource. Add your parameter to the route.

[Authorize]
[HttpGet, Route("api/GetJobType/{id}")]
public IHttpActionResult GetJobType(int? id)
{
   try
   {
     var model = new JobsModel();
     return Ok(model.GetJobType(id.Value));
   }
   catch
   {
     return InternalServerError();
   }
}

Upvotes: 2

Related Questions