GibboK
GibboK

Reputation: 73918

How to setup routes.MapRoute with a custom route?

Using asp.net MVC 3, I have in Global.asax

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

I would like the route be from CustomRouteHandler.

public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        CustomHttpHandler handler = new CustomHttpHandler(requestContext);
        return handler;
    }
}

How do I change my routes.MapRoute code?

Upvotes: 1

Views: 1748

Answers (2)

Kenneth
Kenneth

Reputation: 28737

Routes.MapRoute is really just a shorthand method. In case you have a custom handler you can't use the shortcut, you have to use the add-method:

Route specialroute= new Route("path", new CustomRouteHandler());
routes.Add("special", specialroute);

Upvotes: 1

haim770
haim770

Reputation: 49095

Use:

routes.Add(new Route("CustomPath", new CustomRouteHandler()));

Or:

RouteTable.Routes.Add(new Route("CustomPath", new MvcRouteHandler()));

Upvotes: 1

Related Questions