I'm having a strange issue with MVC 2.0 that I can't find any information about.
Basically, I'm adding ASP.NET 4.0 WebForms URL routing for legacy pages on an application that also has MVC pages. Once I add *any* routes.MapPageRoute to Global.asax.cs,
all of my MVC ActionLinks/etc immediately break and all point to the first "MapPageRoute" that was registered.
If I comment out the first MapPageRoute line, then all of my MVC links suddenly direct to the next MapPageRoute I have defined.
Any idea what's wrong? I checked my route names and none are duplicate, and none of the routes I am adding conflict in any way to my MVC routes. It seems as though I might have some sort of web.config configuration issue?
The Routing is works from top to bottem fashion. Therefore a Rule of Thumb is to put more specfic Routes to to top and more General Routes at bottem. In your above MapPageRoute are top, there fore all your ActionLink starts with MapPageRoute, if it is matched
then renders this rule and stop routing.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
That's true, but I'm not sure why it's matching the MapPageRoutes. My action links define a specific action that doesn't resolve to the first MapPageRoute.
1) Ingoing URLS Algorithm, used to match incoming url to handler.
2) Outgoing URLS Algorithm, used to generate URLS. Html.ActionLink, Url.Action, etc used this method to genrate URLs.
Since your MapPageRoute does not contains any Route parameter therefore it will match always and all remaining Routes are ignored. You can resolve this by putting all MapPageRoute at very bottem.
Please read the topic "Understanding the Outbound URL-Matching Algorithm" in Steve book.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Unfortunately I can't place the route at the bottom, since one of the webforms routes has more specific parameters than the MVC parameters.
There's no way at all to mix the MapPageRoutes in the middle of the MapRoutes?
In this scenrio constraint will helps you, All you need is to use this in global.asax,
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Route item = new Route("Donate", null, new RouteValueDictionary (new{ controller = new PageConstraint() }), null, new PageRouteHandler("~/Legacy/Core/Donate.aspx", true));
routes.Add("Donate", item);
and Add the constrint which simply return false for outgoing Urls(so that all your Html helpers will work as expected),
public class PageConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection==RouteDirection.IncomingRequest)
return true;
return false;
}
}
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by ShadowChaser on Aug 15, 2010 07:59 AM
ShadowChaser
Member
69 Points
51 Posts
All MVC links broken after added one MapPageRoute
Aug 13, 2010 02:57 AM|LINK
I'm having a strange issue with MVC 2.0 that I can't find any information about.
Basically, I'm adding ASP.NET 4.0 WebForms URL routing for legacy pages on an application that also has MVC pages. Once I add *any* routes.MapPageRoute to Global.asax.cs, all of my MVC ActionLinks/etc immediately break and all point to the first "MapPageRoute" that was registered.
If I comment out the first MapPageRoute line, then all of my MVC links suddenly direct to the next MapPageRoute I have defined.
Any idea what's wrong? I checked my route names and none are duplicate, and none of the routes I am adding conflict in any way to my MVC routes. It seems as though I might have some sort of web.config configuration issue?
MVC AddPageRoute Routing
andrewjboyd
Contributor
4426 Points
864 Posts
Re: All MVC links broken after added one MapPageRoute
Aug 13, 2010 07:15 AM|LINK
Can you show us what your Routes look like? It shoulds like they conflict
================================================
Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
ShadowChaser
Member
69 Points
51 Posts
Re: All MVC links broken after added one MapPageRoute
Aug 13, 2010 01:41 PM|LINK
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Core
routes.MapPageRoute("Donate", "Donate", "~/Legacy/Core/Donate.aspx");
routes.MapPageRoute("Links", "Links", "~/Legacy/Core/Links.aspx");
routes.MapRoute("Disclaimer", "Disclaimer", new { controller = "Core", action = "Disclaimer" });
routes.MapRoute("TermsOfUse", "TermsOfUse", new { controller = "Core", action = "TermsOfUse" });
// Membership
routes.MapPageRoute("SignIn", "Members/Signin.aspx", "~/Legacy/Core/Signin.aspx");
routes.MapRoute("SignOut", "Members/Signout.aspx", new { controller = "Membership", action = "SignOut" });
routes.MapRoute("DisplayProfile", "Users/{user}", new { controller = "Membership", action = "DisplayProfile" });
// Trails
routes.MapRoute(
"TrailsHome",
"Trails",
new { controller = "Trails", action = "Home" });
routes.MapPageRoute(
"TrailsAdd",
"Trails/Add",
"~/Legacy/Trails/SubmitRoute.aspx");
routes.MapRoute(
"TrailsListChanges",
"Trails/Changes",
new { controller = "Trails", action = "ListChanges" });
routes.MapRoute(
"TrailsDisplayRevision",
"Trails/Revisions/{revision}",
new { controller = "Trails", action = "DisplayRevision" },
new { revision = @"\d+" });
routes.MapPageRoute(
"TrailsEditRevision",
"Trails/Revisions/{revision}/Edit",
"~/Legacy/Trails/SubmitRoute.aspx",
false, null, new RouteValueDictionary() {{ "revision", @"\d+" }});
routes.MapRoute(
"TrailsDownloadWaypoints",
"Trails/Revisions/{revision}/Waypoints",
new { controller = "Trails", action = "DownloadWaypoints" },
new { revision = @"\d+" });
routes.MapPageRoute(
"TrailsEditWaypoints",
"Trails/Revisions/{revision}/Edit",
"~/Legacy/Trails/UploadWaypoints.aspx",
false, null, new RouteValueDictionary() {{ "revision", @"\d+" }});
routes.MapRoute(
"TrailsListByPark",
"Trails/{province}/Parks/{park}",
new { controller = "Trails", action = "ListByPark" });
routes.MapRoute(
"TrailsListChangesByPark",
"Trails/{province}/Parks/{park}/Changes",
new { controller = "Trails", action = "ListChangesByPark" });
routes.MapRoute(
"TrailsListByRegion",
"Trails/{province}/{region}",
new { controller = "Trails", action = "ListByRegion" });
routes.MapRoute(
"TrailsListChangesByRegion",
"Trails/{province}/{region}/Changes",
new { controller = "Trails", action = "ListChangesByRegion" });
routes.MapRoute(
"TrailsDisplayEntry",
"Trails/{province}/{region}/{entry}",
new { controller = "Trails", action = "DisplayEntry" });
routes.MapRoute(
"TrailsListRevisions",
"Trails/{province}/{region}/{entry}/Revisions",
new { controller = "Trails", action = "ListRevisions" });
// Legacy routes
routes.MapRoute(
"LegacyTrailsHomeDefault",
"Routes",
new { controller = "Legacy", action = "TrailsHome" });
routes.MapRoute(
"LegacyTrailsHome",
"Routes/Default.aspx",
new { controller = "Legacy", action = "TrailsHome" });
routes.MapRoute(
"LegacyTrailsListByRegion",
"Routes/ByRegion.aspx",
new { controller = "Legacy", action = "ListTrailsByRegion" });
routes.MapRoute(
"LegacyTrailsListByPark",
"Routes/ByPark.aspx",
new { controller = "Legacy", action = "ListTrailsByPark" });
routes.MapRoute(
"LegacyTrailsDisplay",
"Routes/Route.aspx",
new { controller = "Legacy", action = "DisplayTrail" });
imran_ku07
All-Star
45847 Points
7710 Posts
MVP
Re: All MVC links broken after added one MapPageRoute
Aug 14, 2010 03:34 PM|LINK
The Routing is works from top to bottem fashion. Therefore a Rule of Thumb is to put more specfic Routes to to top and more General Routes at bottem. In your above MapPageRoute are top, there fore all your ActionLink starts with MapPageRoute, if it is matched then renders this rule and stop routing.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ShadowChaser
Member
69 Points
51 Posts
Re: All MVC links broken after added one MapPageRoute
Aug 14, 2010 03:43 PM|LINK
That's true, but I'm not sure why it's matching the MapPageRoutes. My action links define a specific action that doesn't resolve to the first MapPageRoute.
imran_ku07
All-Star
45847 Points
7710 Posts
MVP
Re: All MVC links broken after added one MapPageRoute
Aug 14, 2010 05:41 PM|LINK
I think you need to understand Routing.
There are two concepts in Routing.
1) Ingoing URLS Algorithm, used to match incoming url to handler.
2) Outgoing URLS Algorithm, used to generate URLS. Html.ActionLink, Url.Action, etc used this method to genrate URLs.
Since your MapPageRoute does not contains any Route parameter therefore it will match always and all remaining Routes are ignored. You can resolve this by putting all MapPageRoute at very bottem.
Please read the topic "Understanding the Outbound URL-Matching Algorithm" in Steve book.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ShadowChaser
Member
69 Points
51 Posts
Re: All MVC links broken after added one MapPageRoute
Aug 14, 2010 06:00 PM|LINK
Ahhh, I'm still a bit new to routing and I forgot about the separate outgoing algorithm.
Unfortunately I can't place the route at the bottom, since one of the webforms routes has more specific parameters than the MVC parameters.
There's no way at all to mix the MapPageRoutes in the middle of the MapRoutes?
imran_ku07
All-Star
45847 Points
7710 Posts
MVP
Re: All MVC links broken after added one MapPageRoute
Aug 14, 2010 06:44 PM|LINK
In this scenrio constraint will helps you, All you need is to use this in global.asax,
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Route item = new Route("Donate", null, new RouteValueDictionary (new{ controller = new PageConstraint() }), null, new PageRouteHandler("~/Legacy/Core/Donate.aspx", true));
routes.Add("Donate", item);
and Add the constrint which simply return false for outgoing Urls(so that all your Html helpers will work as expected),
public class PageConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection==RouteDirection.IncomingRequest)
return true;
return false;
}
}
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ShadowChaser
Member
69 Points
51 Posts
Re: All MVC links broken after added one MapPageRoute
Aug 15, 2010 07:59 AM|LINK
I'll give that a try, thanks!