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
{
}
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.
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.
The three segments of a default route contain the Controller, Action and Id.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With