I am developing a .NET 3.5 Web Forms based website that uses URL Routing. So far I have created a few routes and I have had no issue. I now have a .ashx file that is going to handle sending .pdf files from a table in SQL Server to the website when someone clicks on a link. Normally when I create a Handler it would look like this:
return BuildManager.CreateInstanceFromVirtualPath("~/ViewItem.aspx", typeof(Page)) as Page;
For my .ashx file I tried:
return BuildManager.CreateInstanceFromVirtualPath("~/FileServer.ashx", typeof(Page)) as Page;
This doesn’t work though because fileserver.ashx is not a page so casting it as typeof(Page)) as Page is going to fail. What do I cast the VirtualPath as instead of Page or is there some other way I should be doing this.
use
typeof(IHttpHandler)instead oftypeof(Page)That is the base class of the ashx file Also change the return type from as Page to as IHttpHandler. Then update any other code that depends on it being a page.