I am new to ASP.NET MVC3.
I have configured some routes in Global.asax, against which I am generating some hyperlinks using @Html.ActionLink helper method.
All of the links are getting correctly rendered except the top one in the below code:
Global.asax
routes.MapRoute(
null,
"Section/{Page}/{SubPage}/{DetailPageName}",
new { controller = "Base" }
);
routes.MapRoute(
null,
"Section/{Page}/{SubPage}",
new { controller = "Base", action = "SubPage" }
);
routes.MapRoute(
null,
"Section/{Page}",
new { controller ="Base", action="LandingPage"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Base", action = "Index" } // Parameter defaults
);
ActionLink code
@Html.ActionLink(@subPages.LinkedPageName, "DetailPage",
new {
Controller = "Base",
Page = @ViewBag.PageName,
SubPage = @Model.SubPageName,
DetailPageName = subPages.LinkedPageName
})
The above should pick the top route i.e.:
routes.MapRoute(
null,
"Section/{Page}/{SubPage}/{DetailPageName}",
new { controller = "Base" }
);
But it is picking the default route!
In this route definition:
routes.MapRoute(
null,
"Section/{Page}/{SubPage}/{DetailPageName}",
new { controller = "Base" }
);
The following conditions must be satisfied in order for the route to match:
controller
parameter passed into ActionLink
then its value must be Base
Page
parameter must be specified and must be non-empty because it has no default valueSubPage
parameter must be specified and must be non-empty because it has no default valueDetailPageName
parameter must be specified and must be non-empty because it has no default valueSo in this call to ActionLink
:
@Html.ActionLink(@subPages.LinkedPageName, "DetailPage",
new {
Controller = "Base",
Page = @ViewBag.PageName,
SubPage = @Model.SubPageName,
DetailPageName = subPages.LinkedPageName
})
Condition #1 is clearly satisfied. But conditions #2, #3, and #4 might not be satisfied because their values might be null.
And because you state that the route that ends up matching is the default route, I suspect that the Page
parameter is null or empty. that is, @ViewBag.PageName
is returning a null or empty value.
Check in your code (perhaps in the debugger or print it out in the view) to see whether the PageName
property has a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With