【问题标题】:URL routing requires /Home/Page?page=1 instead of /Home/Page/1URL 路由需要 /Home/Page?page=1 而不是 /Home/Page/1
【发布时间】:2016-02-18 03:44:32
【问题描述】:

我正在尝试构建我的 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

如果重要,此链接指向的控制器操作如下:

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>

谁能告诉我哪里出错了?

【问题讨论】:

  • 方法改成public ActionResult Page(int id)或者路由改成url: "{controller}/{action}/{page}/{title}", 并且注意只有最后一个参数可以标记为UrlParameter.Optional
  • 呃...您在问一个主题 SEO 标记的问题?那一定是错的。不知何故。

标签: c# asp.net-mvc url seo maproute


【解决方案1】:

代替

<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);
}

【讨论】:

  • 感谢您的帮助,这已解决:)
猜你喜欢
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多