我想创建Url像www.mysite.com/home/details/1/lesson1在mvc与Url.action
本文关键字:Url lesson1 action details mvc 创建 www mysite com home | 更新日期: 2023-09-27 18:16:17
我想在mvc中创建url与Url.Action
我的代码是
Url.Action( "Details", "Home", new { id= item.Id ,title=item.Title}, "http" )
但是这段代码创建的链接类似于
http://localhost:45201/Admin/Home/Details/1?title=lesson1
我想在
http://localhost:45201/Admin/Home/Details/1/lesson1
取决于你如何定义你的路由。
用于基于约定的路由
routes.MapRoute(
name: "AdminHomeDetails",
url: "Admin/Home/Details/{id}/{title}",
defaults: new { controller = "Home", action = "Details" }
);
或使用此属性路由:
[RoutePrefix("Admin/Home")]
public class HomeController : Controller {
//GET Admin/Home/Details/1/lesson1
[Route("Details/{id:int}/{title}")]
public ActionResult Details(int id, string title) { ... }
}
对于属性路由不要忘记在基于约定的路由之前映射属性路由
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//...and then convention-based routes.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" }
);
}
}
检查ASP中的属性路由。