I have a problem that I don't know why is ocurring.
What I want is to call a method in my web api, the httpOptions verb should return me a HttpOkStatus but instead it returns a 404 error. This is only happening if I put the tag
[Route("MyRoute")]
, otherwise it work perfectly.
My question is, why it does not work when I put the tag on it?.
I have this code
[Authorize]
[RoutePrefix("api/MergedSchemas")]
public class MergedSchemasController : ApiController
{
[HttpGet]
[Route("Schemas")]
public HttpResponseMessage GetSchemas(string formName)
{
//Some code
}
}
In the Global.asax I have this code:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.MessageHandlers.Add(new OptionsHttpMessageHandler());
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
public class OptionsHttpMessageHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Options)
{
var apiExplorer =
GlobalConfiguration.Configuration.Services.GetApiExplorer();
var controllerRequested = request.GetRouteData().Values["controller"] as string;
var supportedMethods = apiExplorer.ApiDescriptions.Where(d =>
{
var controller = d.ActionDescriptor.ControllerDescriptor.ControllerName;
return string.Equals(
controller, controllerRequested, StringComparison.OrdinalIgnoreCase);
})
.Select(d => d.HttpMethod.Method)
.Distinct();
if (!supportedMethods.Any())
return Task.Factory.StartNew(
() => request.CreateResponse(HttpStatusCode.NotFound));
return Task.Factory.StartNew(() =>
{
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Headers.Add("Access-Control-Allow-Methods", string.Join(",", supportedMethods));
return resp;
});
}
return base.SendAsync(request, cancellationToken);
}
}
The uri I am calling is : /api/MergedSchemas/Schemas?formName=frmBCOSEG
Thank you!