改变MVC全局生成的URL

时间:2013-05-02 18:23:20

标签: c# asp.net-mvc url routing seo

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

我只是希望能够在MVC.NET中更改由助手类创建的每个URL,问题是我不知道我必须覆盖什么才能这样做。

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

Google&我不是更聪明......

如果有人知道我需要改变什么,请给我一个提示,谢谢:)

2 个答案:

答案 0 :(得分:1)

掌握路线:

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

路径可在Global.asax文件中找到,而像ActionLink这样的HtmlHelpers应自动适应您的自定义路由。如果您在默认路由上指定自定义路由,然后执行以下操作,则生成的URL将与路径匹配:

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

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

@Html.ActionLink("Go!", "AnAction", "AController",
    new { MyCustomParameter = "Hello" })

答案 1 :(得分: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);
        }
    }
}

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

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