2013-10-09 129 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 

然後,你還必須有一個幫手來替換每一個連字符與網址參數的標題,你可能會傳遞給控制器​​動作後,以查詢後一個數據庫中的空白。