قابلیت Attribute Routing در ASP.NET MVC 5
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۲/۰۹/۱۷ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
// ...
} [RoutePrefix("products")]
public class ProductsController : Controller
{
public ProductsController()
{
}
[Route]
public ActionResult Index()
{
return View();
}
} [Route("{id?}")]
public ActionResult Index(int id)
{
return View();
} [RoutePrefix("products")]
[Route("{action=index}")]
public class ProductsController : Controller
{
public ProductsController()
{
}
public ActionResult Index()
{
return View();
}
} [Route("{id:int}")]
public ActionResult Delete(int id)
{
return View();
} [Route("{title:regex(\u0600-\u06FF)}")]
public ActionResult Search(string title)
{
return View();
} ضمن تشکر از مطلب خوبتون از این قابلیت چگونه برای آدرس دهی اتوماتیک استفاده کرد؟
برای مثال در بالا اکشن Index یک پارامتر Id دارد . چطوری میتوان با ذکر products و Id مستقیما به اکشن Index فوروارد بشه یعنی : http://localhost/products/10
این سوال و بدون قابلیت مذکور و در Mvc4 چطوری صحیحتر است انجام شود؟
[Route("{action=index}")]