Dưới đây là một cách khác để Standar đăng ký tuyến đường:
1. Tải RiaLibrary.Web.dll và tham khảo nó trong dự án trang web ASP.NET MVC của bạn
2. Trang trí các phương pháp điều khiển với các thuộc tính [Url]:
public SiteController : Controller
{
[Url("")]
public ActionResult Home()
{
return View();
}
[Url("about")]
public ActionResult AboutUs()
{
return View();
}
[Url("store/{?category}")]
public ActionResult Products(string category = null)
{
return View();
}
}
BTW, '?' đăng nhập tham số '{? category}' có nghĩa là tham số là tùy chọn. Bạn sẽ không cần phải xác định này một cách rõ ràng trong giá trị mặc định tuyến đường, đó là tương đương như sau: nộp
routes.MapRoute("Store", "store/{category}",
new { controller = "Store", action = "Home", category = UrlParameter.Optional });
3. Cập nhật Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoutes(); // This do the trick
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
Làm thế nào để thiết lập giá trị mặc định và hạn chế? Ví dụ:
public SiteController : Controller
{
[Url("admin/articles/edit/{id}", Constraints = @"id=\d+")]
public ActionResult ArticlesEdit(int id)
{
return View();
}
[Url("articles/{category}/{date}_{title}", Constraints =
"date=(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])")]
public ActionResult Article(string category, DateTime date, string title)
{
return View();
}
}
Làm thế nào để thiết lập trật tự? Ví dụ:
[Url("forums/{?category}", Order = 2)]
public ActionResult Threads(string category)
{
return View();
}
[Url("forums/new", Order = 1)]
public ActionResult NewThread()
{
return View();
}
này là một tiện ích hữu ích quá: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx –
Hi Kevin, bạn đã nhận được câu trả lời của bạn ?? – Parminder
Kiểm tra câu hỏi này: [Tính năng Định tuyến Web hoạt động như thế nào?] (Http://stackoverflow.com/questions/43243/how-does-web-routing-work#43363) – jfs