开发者

Using URL Routing for Web Forms with .ashx files

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 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜