What is the difference between route.MapMvcAttributeRoutes () and context.Routes.MapMvcAttributeRoutes ();
I have an mvc app with an additional scope named account
I use MvcSiteMapProvider
to make bread crumbs
I have an activity that returns data for a specific invoice. This action URL is similar to localhost/account/profile/invs-histr/details/ID
where ID is the invoice ID.
I have accountAreaRegistration.cs
for registering area routes and I have RouteConfig.cs
for registering global routes.
I currently need to register a route for localhost/account/profile/invs-histr/details/ID
in both files. If I don't register this route in accountAreaRegistration.cs
, I have 404 exception
. If I don't register this route in RouteConfig.cs
, the breadcrumbs are not displayed.
Start of file RouteConfig.cs
:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
}
And accountAreaRegistration.cs
:
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapMvcAttributeRoutes();
}
Can someone explain to me what is the difference between routes.MapMvcAttributeRoutes()
and context.Routes.MapMvcAttributeRoutes()
?
Why would I need to register a route in both files?
Thank you in advance
This is an extension method, so the object from which you call it is the first parameter. This parameter is RouteCollection
, and this collection is different in both cases:
- The first call registers controller routes "at the root" of the application that have routing attributes
- The second call does the same for the controllers inside the current scope
If you want to avoid a second call for each area, you can decorate your controllers with an attribute [RouteArea("AreaName")]
.
If you'd like to better understand attribute routing, see this document: Attribute Routing in ASP.NET MVC 5 Pay special attention to the Scopes section.