6

Using the library AttributeRouting, I was able to configure attribute routing to use a custom route handler (inheriting MvcRouteHandler):

routes.MapAttributeRoutes(cfg =>
    {
        cfg.UseRouteHandler(() => new MultiCultureMvcRouteHandler());
    }
);

Also, before MVC5, it was possible to change the route handler of any existing route:

(routes["myroute"] as Route).RouteHandler = new MyCustomRouteHandler();

With MVC5 using attribute routing, the routes collection contains internal classes (RouteCollectionRoute for example) and it doesn't seem to be possible to change the route's RouteHandler property.

How can I change the default route handler used when working with attribute routing in MVC5.1 ?

marapet
  • 54,856
  • 12
  • 170
  • 184
  • Do you mean route constraints? – Steve Andrews Mar 24 '14 at 07:30
  • @SteveAndrews No, this question is about changing the RouteHandler. But it looks like this isn't possible, at least not with attribute routing in MVC5.1 :( – marapet Mar 24 '14 at 09:29
  • Yeah the RouteHandler can definately still be changed, sounds like that library just needs to be updated. – JuhaKangas Mar 28 '14 at 09:13
  • @JuhaKangas If you are able to change the routehandler of a route created with attribute routing in MVC 5.1 (_not_ the excellent AttributeRouting library which existed well before MVC5), please add an answer to the question and show me. – marapet Mar 28 '14 at 10:19
  • Ok, sorry, I got it a bit wrong. I provided an answer for you, I hope it helps. – JuhaKangas Mar 28 '14 at 12:39

1 Answers1

0

Create your own RouteAttribute.

Check the docs here: http://msdn.microsoft.com/en-us/library/system.web.mvc.routeattribute(v=vs.118).aspx

Implement those interfaces and in the CreateRoute method you can choose you routehandler for the RouteEntry object.

I haven't tried it out but something like the below, you need to do some more work but that should put you on track.

public class MyRouteAttribute : Attribute, IDirectRouteFactory, IRouteInfoProvider
{
    public RouteEntry CreateRoute(DirectRouteFactoryContext context)
    {
        return new RouteEntry("Test", new Route("Url", new CustomRouteHandler()));
    }

    public string Name
    {
        get { throw new NotImplementedException(); }
    }

    public string Template
    {
        get { throw new NotImplementedException(); }
    }
}
JuhaKangas
  • 875
  • 5
  • 17
  • MapMvcAttributeRoutes() will map your custom route attribute automatically when using these interfaces by the way. – JuhaKangas Mar 28 '14 at 12:45
  • But this would mean not only to use the custom attribute everywhere, but also to reimplement whatever DirectRouteFactoryContext is doing. Looks like it got even more complicated ;) – marapet Mar 28 '14 at 16:21
  • I can only agree with you, I buy having to use a custom attribute but reimplementing CreateRoute doesn't exactly seem great. An inheritable routeattribute (it's sealed right now) that would allow setting the routehandler would have been nice. But yeah, this is the only option I was able to find right now. – JuhaKangas Mar 28 '14 at 22:14