When creating a new MVC project in Visual Studio, the navigation bar that is created in the _Layout.cshtml
file contains the link shown below. It serves the purpose of being the "Home" button for your application, and the area=""
value indicates the default area.
It works fine enough, but I was wondering if it possible to replace ""
with a named value.
@Html.ActionLink("Sample", "Index", "Home",
new { area = "" }, new { @class = "navbar-brand" })
I looked at the default route and didn't see any parameters for area.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);
Is there some easy tweak I can make so that area=""
can be written as `area="default", or some other non-empty value?
When using ActionLink/RouteLink/UrlHelper there is a special case where you have to put the area
into the route values collection.
However, under the covers MVC actually uses DataTokens["area"]
to determine what area to match. So if you are comparing against the current request, you could check whether DataTokens["area"]
exists - if not, it indicates a request for the default area.
You can also set that parameter on the routes in your route configuration if you want them to be specific to an area.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
).DataTokens["area"] = "default";