我正在尝试构建我的 ASP.NET MVC 4.5 项目以使用搜索引擎友好的 URL。我正在使用以下路由映射。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
);
目的是让我可以创建这样的 URL:
Mysite.com/Home/Page/1/this-title-bit-is-just-for-show
但它失败了,我不得不使用这样的 URL:
Mysite.com/Home/Page?page=1
如果重要,此链接指向的 Controller 操作如下:
public ActionResult Page(int page)
{
PostModel pm = new PostModel(page);
return View(pm);
}
我正在生成这样的 URL:
<a href="@Url.Action("Page", "Home", new { page = 1 })">1</a>
谁能告诉我哪里出错了?
最佳答案
代替
<a href="@Url.Action("Page", "Home", new { page = 1 })">1</a>
使用
<a href="@Url.Action("Page", "Home", new { id = 1 })">1</a> //instead of page use id here
并如图所示更改操作方法:-
public ActionResult Page(int id) //instead of page use id here
{
PostModel pm = new PostModel(id);
return View(pm);
}
关于c# - URL 路由需要/Home/Page?page=1 而不是/Home/Page/1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35472431/