1

Is there a way add a fourth route token to the existing area, controller, action routes?

for example i want a routing like

fourthRouteValue/Area/Controller/Action

The app we are trying to build is so large that it will be almost impossible to properly arrange everything without it.

Thanks in advance.

2
  • Am finding it hard understanding the routing example above. For asp.net mvc though, it is possible to create custom route templates that you can re-use in your controllers. Check this out learn.microsoft.com/en-us/aspnet/core/mvc/controllers/… Sep 8, 2018 at 1:03
  • Th current ASP NET Core 2.0 routes use Area, Controller and Action as route tokens. But our app is so large that a fourth token would be a very big help. And there are too many actions to go around and give routes as the as per the document. Sep 9, 2018 at 6:15

1 Answer 1

4

So for anyone who comes here later, here is how i fixed it.

I created a RouteValueAttribute as follows

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RegionAttribute : RouteValueAttribute
{
    public RegionAttribute(string regionName) : base("Region", regionName)
    {
    }
}

Then i can use this similar to how you would specify an area

[Region("Region")]
[Area("Area")]
public class TestController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

and in startup.cs

app.UseMvc(routes =>
{
    routes.MapRoute(
         name: "main_route_region",
         template: "{region:exists}/{area:exists}/{controller=Home}/{action=Index}/{id?}");
});

Now the action will be accessible from ~/Region/Area/Test/Index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.