WCF Webapi UriTemplate with file extension works local but not on IIS7
-
31-05-2021 - |
Question
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?
Solution 2
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>
OTHER TIPS
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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow