0

I have a WCF WebApi Rest service that has the following endpoints:

[WebGet(UriTemplate = "{id}")]

and

[WebGet(UriTemplate = "{id}.pdf")]

The first endpoint returns JSON and the second endpoint returns a pdf. Both of these endpoints work in my local environment, but the pdf endpoint returns a 404 on the server running IIS7.

Is there some sort of setup IIS7 that is needed in order for the route to get executed?

Hai Nguyen
  • 4,059
  • 1
  • 20
  • 16

2 Answers2

0

You might need to add .pdf to the MIME TYPE's in IIS

try adding file extension .PDF with a type of application/octet-stream

http://technet.microsoft.com/en-us/library/cc725608%28v=ws.10%29.aspx

UPDATE

To return a dynamically generated PDF directly using something like itextsharp:

[WebGet(UriTemplate = "GetPDF/{id}")]        
public void GetPDF(int id)
        {
        Invoice i = InvoiceData.GetInvoiceByID(id);
        MyApp.Data.Export.PDF pdf = new MyApp.Data.Export.PDF();
        byte[] data = pdf.generatePDFBytes(id);

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=\"" + i.InvoiceNumber + ".pdf" + "\"");
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(data.ToArray());
        Response.End();
    }
Matt Mombrea
  • 6,793
  • 2
  • 20
  • 20
  • This mime type is setup - I think the problem has to do with IIS trying to serve up a static file instead of routing the request. – Hai Nguyen Apr 16 '12 at 17:10
  • Ya, you might be right. Are you able to just change the route to not use the extension and pass the parameter in the path like `[WebGet(UriTemplate = "{id}/pdf")]`? – Matt Mombrea Apr 16 '12 at 20:40
  • I'm actually trying to implement this solution (http://stackoverflow.com/a/7347522/1330215) so that I can directly link to a dynamically generated pdf (ex: Your Invoice). – Hai Nguyen Apr 16 '12 at 21:08
  • I've done something similar where I created a function to return the dynamically generated PDF as the server response so that it could be opened or downloaded directly. I've modified the above answer to show an example of that function. – Matt Mombrea Apr 16 '12 at 21:14
  • Thanks for the update Matt. Generating the pdf isn't an issue for me - it's actually working locally and returns a beautiful pdf. :) The problem happens when I make the same request on the server and instead of executing the route, it returns a 404. – Hai Nguyen Apr 16 '12 at 21:32
0

I found the solution to this problem. It is a simple web.config addition:

<system.webServer>
  <handlers>
      <add name="PDFHandler-Integrated-4.0" path="*.pdf" verb="GET" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" preCondition="integratedMode" />
  </handlers>
</system.webServer>
Hai Nguyen
  • 4,059
  • 1
  • 20
  • 16