I\'m wondering what is the best way to handle URL in MVC. For example, in my application I have a PageController
can link to /website/Page/Index/3
I took a look at http://www.asp.net/learn/mvc/tutorial-23-cs.aspx and I got it working. More simple than I thought...
My routes :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Pages3", "{url1}/{url2}/{url3}", MVC.Page.RedirectTo(), new { url1 = "", url2 = "", url3 = "" });
routes.MapRoute("Pages2", "{url1}/{url2}", MVC.Page.RedirectTo(), new { url1 = "", url2 = "", url3 = "" });
routes.MapRoute("Pages1", "{url1}", MVC.Page.RedirectTo(), new { url1 = "", url2 = "", url3 = "" });
}
And now my controller :
public virtual ActionResult RedirectTo(string url1, string url2, string url3)
{
if (string.IsNullOrEmpty(url1)) return Home();
var pageModel = new PageModel();
pageModel.CurrentPage = _pageRepo.GetByUrl(url1, url2, url3);
BuildMenusAndBreadCrumb(pageModel);
ViewData.Model = pageModel;
return View(Views.Index);
}
And here's how I render a link (my menu exemple) :
讨论(0)