tags:

views:

44

answers:

2

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.

+1  A: 

Hey,

It's an HTTP Handler, so you probably could use its IHttpHandler interface type to cast. But you can't use page; you have to use the type the handler inherits from.

Brian
Both of your answers were similar and worked. Thanks!
RandomBen
+2  A: 

use typeof(IHttpHandler) instead of typeof(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.

Chris Lively