0

我有我的要求将我的 URL 生成为

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

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

asp-net-mvc-framework-part-2-url-路由

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

ETC...

我已经实现了如下: -

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

我究竟做错了什么 ?请有人指出它。

谢谢!

4

1 回答 1

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

然后,您还必须有一个助手,用 url 参数标题中的空格替换每个连字符,您可能会将其传递给控制器​​操作 Post 以查询数据库中的 Post。

于 2014-03-05T06:36:21.233 回答