Use custom route handler with MVC5 attribute routing
Asked Answered
U

1

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 ?

Ultramicroscope answered 13/3, 2014 at 10:15 Comment(5)
Do you mean route constraints?Sacramentarian
@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 :(Ultramicroscope
Yeah the RouteHandler can definately still be changed, sounds like that library just needs to be updated.Krusche
@Krusche 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.Ultramicroscope
Ok, sorry, I got it a bit wrong. I provided an answer for you, I hope it helps.Krusche
K
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(); }
    }
}
Krusche answered 28/3, 2014 at 12:39 Comment(3)
MapMvcAttributeRoutes() will map your custom route attribute automatically when using these interfaces by the way.Krusche
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 ;)Ultramicroscope
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.Krusche

© 2022 - 2024 — McMap. All rights reserved.