2013-10-09 185 views
0

我有我要求我的生成URL作为MVC博客URL路由

/2013/10/custome-mvc-url-rout-to-display-mixture-of-id-and-urlslug 

我已经看到了很多问题来实现它&我的问题可能有重复的可能性。像: -

asp-net-mvc-framework-part-2-url-routing

custome-mvc-url-rout-to-display-mixture-of-id-and-urlslug

等等

我已经如下实现它: -

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
routes.MapRoute(
    "Post", 
    "{year}/{month}/{title}", 
    new { controller = "Blog", action = "Post" } 
    ); 

和我的超链接这将产生,这将是: -

@Html.ActionLink("continue...", "post", "blog", 
new { 
    year = Model.PostedOn.Year, 
    month = Model.PostedOn.Month, 
    day = Model.PostedOn.Day, 
    title = Model.UrlSlug 
}, new { title = "continue..." }) 

我的MVC控制器是: -

public ViewResult Post(int year, int month, string title) 
     {} 

但问题在这里是,我得到我的网址为:

http://localhost:2083/blog/post?Year=2013&Month=10&Day=9&title=best_practices_in_programming 

,而不是像: -

http://localhost:2083/blog/post/2013/10/best_practices_in_programming 

我在做什么错?请有人指出它。

Thiks!

+0

你可以发布你的路线?特别是路线的顺序(Post和Default)。我认为路线应该是'routes.MapRoute( “Post”, “blog/post/{year}/{month}/{title}”, new {controller =“Blog”,action =“Post”} );',否则这些段可能会与默认路由冲突。 – shakib

回答

0

我尝试这样做,只要你把这个路线是这样的RouteConfig.cs默认路由之前,它的工作:

routes.MapRoute(
       null, 
       "{year}/{month}/{title}", 
       new { controller = "Blog", action = "Post" }, 
       new { year = @"\d+", month = @"\d+", title = @"[\w\-]*" }); 

你也应该改变其标题为IMO使用连字符,而不是下划线。这是做这件事的好帮手。

#region ToSlug(), AsMovedPermanently 
    public static class PermanentRedirectionExtensions 
    { 
     public static PermanentRedirectToRouteResult AsMovedPermanently 
      (this RedirectToRouteResult redirection) 
     { 
      return new PermanentRedirectToRouteResult(redirection); 
     } 
    } 

    public class PermanentRedirectToRouteResult : ActionResult 
    { 
     public RedirectToRouteResult Redirection { get; private set; } 
     public PermanentRedirectToRouteResult(RedirectToRouteResult redirection) 
     { 
      this.Redirection = redirection; 
     } 
     public override void ExecuteResult(ControllerContext context) 
     { 
      // After setting up a normal redirection, switch it to a 301 
      Redirection.ExecuteResult(context); 
      context.HttpContext.Response.StatusCode = 301; 
      context.HttpContext.Response.Status = "301 Moved Permanently"; 
     } 
    } 



    public static class StringExtensions 
    { 
     private static readonly Encoding Encoding = Encoding.GetEncoding("Cyrillic"); 

     public static string RemoveAccent(this string value) 
     { 
      byte[] bytes = Encoding.GetBytes(value); 
      return Encoding.ASCII.GetString(bytes); 
     } 



     public static string ToSlug(this string value) 
     { 
      if (string.IsNullOrWhiteSpace(value)) 
      { 
       return string.Empty; 
      } 

      var str = value.RemoveAccent().ToLowerInvariant(); 

      str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); 

      str = Regex.Replace(str, @"\s+", " ").Trim(); 

      str = str.Substring(0, str.Length <= 200 ? str.Length : 200).Trim(); 

      str = Regex.Replace(str, @"\s", "-"); 

      str = Regex.Replace(str, @"-+", "-"); 

      return str; 
     } 
    } 
    #endregion 

然后,你还必须有一个帮手来替换每一个连字符与网址参数的标题,你可能会传递给控制器​​动作后,以查询后一个数据库中的空白。