Search code examples
c#restowinasp.net-web-api2attributerouting

Web Api route {id:int}.xlsx is not found


I have an API endpoint that shall export some data as an Excel xlsx file. The URL I try to get is like this: localhost/service/resources/123.xlsx (because I think using this extension is a nice way to do it). But it always gives me a 404 Not Found.

The controller has a route prefix: [RoutePrefix("resources")] and the route attribute on the action is: [Route("{id:int}.xlsx")]

Even when I changed the route to [Route("123.xlsx")] it would not find it. I also tried a regex constraint [Route("{key:regex(\\d+\\.xlsx}")] with no luck.

I suspect it may be grabbed by static file handler, but the web config already has the setting

<modules runAllManagedModulesForAllRequests="true"/>

I believe this should work, because it worked for a very similar route in a previous project, using the old routing setup and not attribute routing. That project was not hosted using OWIN, but is that the problem here?

How can I make this work? I need this route for both GET and PUT.

Btw, localhost/service/resources/123 will return a different representation of the resource.

Info: Hosted in IIS 7.5 using OWIN 3.0 and Web API 5.2.2


Solution

  • I found out here that adding this line of code

    app.UseStageMarker(PipelineStage.MapHandler);
    

    after

    app.UseWebApi()
    

    in the Startup class made it work!

    (I don't actually understand exactly what it does, but hey, who cares)