2

I can see the appropriate HttpRequest in Fiddler but the Controller method is never invoked but the request seems to be intercepted and a 404 is returned. Other controller methods are invoked appropriately.

HttpRequest

GET http://localhost:36696/test/file/69946/FF47F87FE63E6C24644631FAEA15B157/file.pdf HTTP/1.1
Host: localhost:36696
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,sv;q=0.6

Controller method

[Route("test/file/{fileId:int}/{hash}/file.pdf")]
public HttpResponseMessage GetFile(int fileId, string hash)
{
    [..]
}

Questions;

  • How do I properly set the Route (System.Web.HttpRouteAttribute) with embedded parameters?

  • Is there any default filter prohibiting me from accessing PDF files? This is in IIS Express (VS2013)

I cannot post the entire web.config for privacy concerns but there are no httpHandlers or mimeType entries in the file.

2 Answers 2

4

IIS thinks that it is looking for a static file and is not running the request through the Managed Pipeline.

Adding this entry to the web.config under <handlers> will tell IIS to run these requests through the routes/managed pipeline.

   <add
        name="ManagedPDFExtension"
        path="test/file/*/*/*.pdf"
        verb="GET"
        type="System.Web.Handlers.TransferRequestHandler"
        preCondition="integratedMode,runtimeVersionv4.0" />

I included the route pretty specifically in the path so that it doesn't run all of your static content through the full .NET pipeline. If you have lot of these endpoints or serve all files in this way then you can use a more generic path pattern.

Sign up to request clarification or add additional context in comments.

1 Comment

This is actually the solution I chose just seconds prior to reading your answer. Thank you for confirming it!
0

The issue is the "." in the route. The following in the web config will allow it:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    ...
</system.webServer>

I should also add that there appear to be other web.config entries that can affect this as well. This is just the one I have used to solve this problem. A search for "webapi routing with dot" should show you other solutions with deeper explanations as well.

2 Comments

Thank you for your input but this is actually a terrible idea - "anything goes" - enabling all ASP.NET modules to run for every request in my application, regardless of the "managedHandler" precondition, rendering possible security breaches...
Marcus - I just edited my answer. Again, the "." is your problem. As mentioned, there appear to be many ways to handle it - the above does work, but you should be able to find other ways as well. Personally, if you don't need the ".", I would change your routing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.