1

我有一个非常简单的问题。

我只想能够更改由 MVC.NET 中的帮助程序类创建的每个 URL,问题是我不知道我必须重写什么才能这样做。

编辑:我的目标是在创建 url 时改变 MVC 的行为。我想让 MVC 创建 seo 友好的 url。

经过数小时的谷歌和必应之后,我并没有变得更聪明......

如果有人知道我必须在哪里改变一些东西,请给我一个提示,谢谢:)

4

2 回答 2

1

掌握路由:

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

路由位于 Global.asax 文件中,HtmlHelpersActionLink应该会自动适应您的自定义路由。如果您在默认路由之上指定自定义路由,然后执行以下操作,则生成的 URL 将匹配该路由:

@Html.ActionLink("Go!", "AnAction", "AController")

如果您在路由中指定了其他不能作为标准重载的参数的参数ActionLink,您可以在参数中传递这些routeValues参数:

@Html.ActionLink("Go!", "AnAction", "AController",
    new { MyCustomParameter = "Hello" })
于 2013-05-02T18:25:56.503 回答
0
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace yourbelovedNamespace
{
    /// <summary>
    /// Provides properties and methods for defining a seo friendly route
    /// </summary>
    public class SeoFriendlyRoute : Route
    {
        #region Constructors

        public SeoFriendlyRoute(string url, IRouteHandler routeHandler)
            : base(url, routeHandler)
        {
        }

        public SeoFriendlyRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
            : base(url, defaults, routeHandler)
        {
        }

        public SeoFriendlyRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints,
                                IRouteHandler routeHandler)
            : base(url, defaults, constraints, routeHandler)
        {
        }

        public SeoFriendlyRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints,
                                RouteValueDictionary dataTokens, IRouteHandler routeHandler)
            : base(url, defaults, constraints, dataTokens, routeHandler)
        {
        }

        #endregion

        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData data = base.GetRouteData(httpContext);
            return data;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            var trimmedValues = new RouteValueDictionary();
            foreach (var value in values)
            {
                if (value.Value != null && value.Key != "controller" && value.Key != "action")
                    trimmedValues.Add(value.Key, GenerateSeourl(value.Value.ToString()));
                else
                {
                    trimmedValues.Add(value.Key, value.Value != null ? value.Value.ToString() : value.Value);
                }

            }
            VirtualPathData data = base.GetVirtualPath(requestContext, trimmedValues);
            if (data != null)
                data.VirtualPath = data.VirtualPath;
            return data;
        }

        private static string GenerateSeourl(string url)
        {
            // make the url lowercase
            var encodedUrl = (url ?? "");
            encodedUrl = encodedUrl
                .Replace("%20", "")
                .Replace(" ", "")
                .Replace("/", "-");
            return encodedUrl;
        }
    }

    public static class SeoFriendlyRouteCollectionExtensions
    {
        //Override for localized route
        public static Route MapSeoFriendlyRoute(this RouteCollection routes, string name, string url, object defaults)
        {
            return routes.MapSeoFriendlyRoute(name, url, defaults, null, null);
        }

        public static Route MapSeoFriendlyRoute(this RouteCollection routes, string name, string url, object defaults,
                                                object constraints)
        {
            return routes.MapSeoFriendlyRoute(name, url, defaults, constraints, null);
        }

        public static Route MapSeoFriendlyRoute(this RouteCollection routes, string name, string url, object defaults,
                                                object constraints, string[] namespaces)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }
            var item = new SeoFriendlyRoute(url, new MvcRouteHandler())
                           {
                               Defaults = new RouteValueDictionary(defaults),
                               Constraints = new RouteValueDictionary(constraints),
                               DataTokens = new RouteValueDictionary()
                           };
            if ((namespaces != null) && (namespaces.Length > 0))
            {
                item.DataTokens["Namespaces"] = namespaces;
            }
            routes.Add(name, item);
            return item;
            //return routes.MapSeoFriendlyRoute(name, urlPattern, defaults, null);
        }

        private static Route MapSeoFriendlyRoute(this AreaRegistrationContext context, string name, string url,
                                                 object defaults, object constraints, string[] namespaces)
        {
            if ((namespaces == null) && (context.Namespaces != null))
            {
                namespaces = context.Namespaces.ToArray();
            }

            Route route = context.Routes.MapSeoFriendlyRoute(name, url, defaults, constraints, namespaces);
            route.DataTokens["area"] = context.AreaName;
            bool flag = (namespaces == null) || (namespaces.Length == 0);
            route.DataTokens["UseNamespaceFallback"] = flag;
            return route;
        }

        public static Route MapSeoFriendlyRoute(this AreaRegistrationContext context, string name, string url,
                                                object defaults, string[] namespaces)
        {
            return context.MapSeoFriendlyRoute(name, url, defaults, null, namespaces);
        }

        public static Route MapSeoFriendlyRoute(this AreaRegistrationContext context, string name, string url,
                                                object defaults, object constraints)
        {
            return context.MapSeoFriendlyRoute(name, url, defaults, constraints, null);
        }

        public static Route MapSeoFriendlyRoute(this AreaRegistrationContext context, string name, string url,
                                                string[] namespaces)
        {
            return context.MapSeoFriendlyRoute(name, url, null, namespaces);
        }

        public static Route MapSeoFriendlyRoute(this AreaRegistrationContext context, string name, string url,
                                                object defaults)
        {
            return context.MapSeoFriendlyRoute(name, url, defaults, null);
        }

        public static Route MapSeoFriendlyRoute(this AreaRegistrationContext context, string name, string url)
        {
            return context.MapSeoFriendlyRoute(name, url, null);
        }
    }
}

所以这是我解决问题的方法。它允许在区域和默认 RouteConfig 中注册“SEO”路线。

修剪过程不是很成熟,但仅用于演示目的。

于 2013-05-03T10:19:07.543 回答