Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My web site has a handler (FileDownload.ashx) that deals with all file download requests.

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

Type '.Handlers.FileDownload' does not inherit from 'System.Web.UI.Page'.

This makes sense, as routing is only implemented in the page.

What steps do I need to take to be able to use routing and my .ashx together? I want to be able to extract RouteData.Values from the route.

public class FileDownload : IHttpHandler
{
}
like image 943
Leigh Bowers Avatar asked Jun 29 '10 16:06

Leigh Bowers


People also ask

How do I route a URL?

The handler can be a physical file, such as an . aspx file in a Web Forms application. A handler can also be a class that processes the request. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.

Which is the correct default route mapping within MVC?

The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home. action = Index.

What are the 3 important segments for routing?

The three segments of a default route contain the Controller, Action and Id.

Where are routes defined in MVC?

The MVC framework leverages routing to direct a request to a controller. The Global. asax file is that part of your application, where you will define the route for your application. This is the code from the application start event in Global.


2 Answers

I needed to hand craft a handler in the end, but it was easy enough: http://haacked.com/archive/2009/11/04/routehandler-for-http-handlers.aspx

.Net 4.0 does not natively support route handling for IHttpHandlers.

like image 199
Leigh Bowers Avatar answered Sep 24 '22 15:09

Leigh Bowers


Sounds like an IIS problem.

Does this work if you try and use the ASP.NET Development Server (Cassini) ?

If you're using IIS6 you'll need to use Wildcard Application Mappings - see here.

You'll also still need to create your routes as per any ASPX page, like this:

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);
}

Have you done that? If so, i'd say your problem is definetely with IIS.

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

Good luck!

like image 20
RPM1984 Avatar answered Sep 24 '22 15:09

RPM1984