ASP.Net 4.0 - How to access RouteData from ASHX?

My website has a handler (FileDownload.ashx) that deals with all file upload requests.

I recently migrated my site to ASP.NET 4.0, and now it uses routing extensively. Everything works fine when working with page requests (aspx), but does not work with my handler - I encounter the following error:

The type .Handlers.FileDownload does not inherit from System.Web.UI.Page.

This makes sense since routing is only implemented on the page.

What steps should I take to be able to use routing and my .ashx together? I want to be able to retrieve RouteData.Values from a route.

 public class FileDownload : IHttpHandler { } 
+7
ashx routes
source share
2 answers

I needed to process the handler at the end, but it was simple enough: http://haacked.com/archive/2009/11/04/routehandler-for-http-handlers.aspx

.Net 4.0 does not support route support for IHttpHandlers.

+3
source share

Sounds like an IIS issue.

Does this work if you are trying to use an ASP.NET development server (Cassini)?

If you are using IIS6, you need to use wildcard application mappings - see here .

You will also need to create routes according to any ASPX page, for example:

 public static void RegisterRoutes(RouteCollection routes) { string[] allowedMethods = { "GET", "POST" }; HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods); Route fileDownloadRoute = new Route("{foo}/{bar}", new FileDownload()); fileDownloadRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } }; routes.Add(fileDownloadRoute); } 

You did it? If so, I would say that your problem is definitely related to IIS.

See here for a good article on ASP.NET 4 Routing for IIS6 and IIS7.

Good luck

+1
source share

All Articles