Hey all,As per Scott Guthrie's blog (http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx) ...
The beauty of all of the above helper methods is that they enable us to avoid having to hard-code in URL paths within our Controller and View Logic. If at a later point we decide to change the search URL route mapping
rule back from "/Search/[query]/[page]" to "/Search/Results/[query]/[page]" or /Search/Results?query=[query]&page=[page]" we can easily do so by editing it in one place (our route registration code). We don't need to change any code within our views or controllers
to pick up the new URL (this maintaining the
"DRY principle").
I think I just have a simple gap in my knowledge... but wanted to ask that concerning this area of MVC. How are the url's generated from these two helper methods and what happens when there are multiple rules to be applied? I will have the following routing
logic applied to my pages...
1. All root pages are <action>.aspx. (Business requested)
2. Site has 1 or more secure areas such as /secure/<action>.aspx, /admin/<action>.aspx.
3. The site is a CMS basically... so any other url should be routed to my CMSController.
What I've come up with in my Global.ascx.cs file is....
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"CommonRootPages", // Route name
"{action}.aspx", // URL with parameters
new { controller = "Home", action = "", id = "" } // Parameter defaults
);
routes.MapRoute(
"SecureAreaWithPage",
"secure/{action}.aspx",
new { controller = "Secure", action = "", id = "" }
);
routes.MapRoute(
"SecureAreaRoot",
"secure",
new { controller = "Secure", action = "Index", id = "" }
);
routes.MapRoute("UserSiteWithPage",
"{siteurl}/{page}",
new { controller = "UserSite", action = "Index", siteurl = "", page = "" },
new { siteurl = @"[a-zA-Z]{1}[a-zA-Z0-9]{0,19}", page = @"[a-zA-Z0-9]{0,25}" });
routes.MapRoute("UserSiteRoute",
"{siteurl}/",
new { controller = "UserSite", action = "Index", siteurl = "", page="" },
new { siteurl = @"[a-zA-Z]{1}[a-zA-Z0-9]{0,19}" });
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
With appropriate controllers. I notice that if I change my routing rules, the urls generated can be changed (e.g. create rule for no controller, just ~/ACTION and the links change, vs. having a Home/Action format. What i am wondering is, how does this
routing structure affect Html.ActionLink/Url.Action? If someone has the info, or a link that explains in more detail, it would be greatly appreciated. I would assume it goes through the rules the same as if a link was typed in, but want to be certain that
I understand this area so to avoid confusion down the road.
What i am wondering is, how does this routing structure affect Html.ActionLink/Url.Action? If someone has the info, or a link that explains in more detail, it would be greatly appreciated.
You can just look at ASP.NET MVC source codes - it's the better source of information for me personally.
RobSil
I would assume it goes through the rules the same as if a link was typed in, but want to be certain that I understand this area so to avoid confusion down the road.
Yes, the order of rules is very important.
Don't forget to click "Mark as Answer" on the post that helped you.
RobSil
Member
68 Points
136 Posts
Routes and Url.Action/Html.ActionLink
Jan 02, 2009 03:58 PM|LINK
The beauty of all of the above helper methods is that they enable us to avoid having to hard-code in URL paths within our Controller and View Logic. If at a later point we decide to change the search URL route mapping rule back from "/Search/[query]/[page]" to "/Search/Results/[query]/[page]" or /Search/Results?query=[query]&page=[page]" we can easily do so by editing it in one place (our route registration code). We don't need to change any code within our views or controllers to pick up the new URL (this maintaining the "DRY principle").
I think I just have a simple gap in my knowledge... but wanted to ask that concerning this area of MVC. How are the url's generated from these two helper methods and what happens when there are multiple rules to be applied? I will have the following routing logic applied to my pages...
1. All root pages are <action>.aspx. (Business requested)
2. Site has 1 or more secure areas such as /secure/<action>.aspx, /admin/<action>.aspx.
3. The site is a CMS basically... so any other url should be routed to my CMSController.
What I've come up with in my Global.ascx.cs file is....
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "CommonRootPages", // Route name "{action}.aspx", // URL with parameters new { controller = "Home", action = "", id = "" } // Parameter defaults ); routes.MapRoute( "SecureAreaWithPage", "secure/{action}.aspx", new { controller = "Secure", action = "", id = "" } ); routes.MapRoute( "SecureAreaRoot", "secure", new { controller = "Secure", action = "Index", id = "" } ); routes.MapRoute("UserSiteWithPage", "{siteurl}/{page}", new { controller = "UserSite", action = "Index", siteurl = "", page = "" }, new { siteurl = @"[a-zA-Z]{1}[a-zA-Z0-9]{0,19}", page = @"[a-zA-Z0-9]{0,25}" }); routes.MapRoute("UserSiteRoute", "{siteurl}/", new { controller = "UserSite", action = "Index", siteurl = "", page="" }, new { siteurl = @"[a-zA-Z]{1}[a-zA-Z0-9]{0,19}" }); routes.MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); }With appropriate controllers. I notice that if I change my routing rules, the urls generated can be changed (e.g. create rule for no controller, just ~/ACTION and the links change, vs. having a Home/Action format. What i am wondering is, how does this routing structure affect Html.ActionLink/Url.Action? If someone has the info, or a link that explains in more detail, it would be greatly appreciated. I would assume it goes through the rules the same as if a link was typed in, but want to be certain that I understand this area so to avoid confusion down the road.
Thx,
Rob
Augi
Contributor
6730 Points
1142 Posts
Re: Routes and Url.Action/Html.ActionLink
Jan 02, 2009 04:17 PM|LINK