【问题标题】:How to get a nice URL like "http://stackoverflow.com/questions/1074/asp-mvc"如何获得一个不错的 URL,例如“http://stackoverflow.com/questions/1074/asp-mvc”
【发布时间】:2010-11-16 16:11:23
【问题描述】:

谁能帮助我?我正在 ASP.NET MVC 中做一些测试。

我想将漂亮的 URL 实现为 stackoverflow.com 路由系统。

例如: stackoverflow.com/questions/1074/asp-mvc domain.com/id/title

这是我的代码:

  1. 在 Global.asax 中
routes.MapRoute(null,
    "posts/{PostId}/{Title}",
    new { controller = "Posts", action = "Details", post = (string)null },
    new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" }
);
  1. 在视图中:
<%= Html.ActionLink(Model.Title, "Details", new { Model.PostID, Model.Title})%>

通过这些代码,我得到了网址:http://localhost:53171/posts/Details?PostID=5&Title=Test-title

谁能给我建议?

非常感谢。

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    不确定所有 stackoverflow url 的含义,但如果您想要一个干净的 url,例如:

    https://stackoverflow.com/Questions/132/thetitle

    在您的 Global.asax 中:

        routes.MapRoute("PostController",
           "Questions/{post_id}/{post_title}",
           new { controller = "Post", action = "Index" },
           new { post_id = @"\d+", post_title = @"([A-Za-z0-9-]+)" }
        );
    

    在您的控制器中检索值:

        public ActionResult Index(Int32 post_Id, String post_title)
        {
            return View();
        }
    

    要生成正确的 url,请使用 Html.RouteLink:

    <%= Html.RouteLink("The Title", "PostController", new { post_id = 132, post_title = "thetitle" })%>
    

    【讨论】:

      【解决方案2】:

      我认为您需要在帖子标题的路径中添加一些默认值...并确保它们映射通过。您似乎没有“postId”和“title”的默认值,但您有一个不存在的“post”路由值。

      routes.MapRoute(
          "PostDetails",
          "posts/{postId}/{title}",
          new { controller = "Posts", action = "Details", postId = 0, title = "" },
          new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" }
      );
      

      帖子控制器

      public ActionResult Details(int postId, string title)
      {
          //whatever
      }
      

      那么在你看来

      <%= Html.ActionLink(Model.Title, "Details", new { @postId = Model.PostID, @title = Model.Title }) %>
      

      或者

      <%= Html.ActionLink(Model.Title, "Details", "Posts", new { @postId = Model.PostID, @title = Model.Title }, null) %>
      

      我还建议在您的帖子模型上创建一个 TitleSlug 属性。

      例如(代码取自here

      public partial class Post
      {
          public string TitleSlug
          {
              get
              {
                  string str = Title.ToLower();
      
                  str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars       
                  str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space
                  str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
                  str = Regex.Replace(str, @"\s", "-"); // hyphens
      
                  return str;
              }
          }
      }
      

      HTH
      查尔斯

      【讨论】:

        【解决方案3】:

        您在正确的道路上,但您只需将控制器 ActionMethod 参数的名称与 Html.ActionLink 中的对象路由值匹配。

        public ActionResult Index(int PostId, string Title)
        {
            ...
            return View();
        }
        
        <%= Html.ActionLink(Model.Title, "Details", 
            new { PostId = Model.PostID, Title = Model.Title}) %>
        

        【讨论】:

          猜你喜欢
          • 2020-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-05
          • 2010-12-20
          相关资源
          最近更新 更多