Routing 是 ASP.net MVC 将地址映射为 Action 方法的技术。
ASP.net MVC 5 支持一种新类型的路由, 称之为 Attribute Routing 。 顾名思义, Attribute Routing 使用来标记定义路由, Attribute Routing 让你在程序中更好的控制资源地址。
App_Start
目录中的 RouteConfig.cs
;MapMvcAttributeRoutes
方法。RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();//Attribute Routing
//Convention-based Routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
}
}
代码要点
RouteConfig.cs
文件中调用 MapMvcAttributeRoutes
方法;routes.MapMvcAttributeRoutes()
必须在基于约定的路由之前配置。[Route("Pet/{petKey?}")]
PetController.cs
public class PetController : Controller
{
// eg: /Pet
// eg: /Pet/123
[Route("Pet/{petKey?}")]
public ActionResult GetPet(string petKey)
{
return View();
}
}
代码要点
/Pet
和 /Pet/123
都将被路由到 GetPet
方法;上面的路由在浏览器中看起来是这样的:
[Route("Pet/Breed/{petKey=123}")]
PetController.cs
public class PetController : Controller
{
// eg: /Pet/Breed
// eg: /Pet/Breed/528
[Route("Pet/Breed/{petKey=123}")]
public ActionResult GetSpecificPet(string petKey)
{
return View();
}
}
代码要点
/Pet/Breed
和 /Pet/Breed/528
都会被路由到 GetSpecificPet
方法。上面的路由在浏览器中看起来是这样的:
/Booking
BookingController.cs
public class BookingController : Controller
{
// eg: /Booking
[Route("Booking")]
public ActionResult Index() { return View(); }
// eg: /Booking/5
[Route("Booking/{bookId}")]
public ActionResult Show(int bookId) { return View(); }
// eg: /Booking/5/Edit
[Route("Booking/{bookId}/Edit")]
public ActionResult Edit(int bookId) { return View(); }
}
上面的路由在浏览器中看起来是这样的:
[RoutePrefix]
前缀;[RoutePrefix("Booking")]
BookingController.cs
[RoutePrefix("Booking")]
public class BookingController : Controller
{
// eg: /Booking
[Route]
public ActionResult Index() { return View(); }
// eg: /Booking/5
[Route("{bookId}")]
public ActionResult Show(int bookId) { return View(); }
// eg: /Booking/5/Edit
[Route("{bookId}/Edit")]
public ActionResult Edit(int bookId) { return View(); }
}
上面的路由在浏览器中看起来是这样的:
[Route("~/PetBooking")]
BookingController.cs
[RoutePrefix("Booking")]
public class BookingController : Controller
{
// eg: /PetBooking
[Route("~/PetBooking")]
public ActionResult PetBooking() { return View(); }
}
上面的路由在浏览器中看起来是这样的:
[Route]
标记, 并将 Action 作为参数;[Route("{action=index}")]
BookingController.cs
[RoutePrefix("Booking")]
[Route("{action=index}")]
public class BookingController : Controller
{
// eg: /Booking
public ActionResult Index() { return View(); }
// eg: /Booking/Show
public ActionResult Show() { return View(); }
// eg: /Booking/New
public ActionResult New() { return View(); }
}
上面的路由在浏览器中看起来是这样的:
[Route]
标记;BookingController.cs
[RoutePrefix("Booking")]
[Route("{action=index}")]
public class BookingController : Controller
{
// eg: /Booking
public ActionResult Index() { return View(); }
// eg: /Booking/Edit/3
[Route("Edit/{bookId:int}")]
public ActionResult Edit(int bookId) { return View(); }
}
上面的路由在浏览器中看起来是这样的:
[Route("Booking", Name = "Payments")]
BookingController.cs
public class BookingController : Controller
{
// eg: /Booking
[Route("Booking", Name = "Payments")]
public ActionResult Payments() { return View(); }
}
Url.RouteUrl
方法来生成链接;<a href="@Url.RouteUrl("Payments")">Payments Screen</a>
注意 : 在上面的代码中, “Payments” 是路由的名称。
原文地址 (需要翻墙), 还有 Attribute Routing in Web API 2, Create a REST API with Attribute Routing in Web API 2