使URL小写。 任何简单或内置的方式?
是否有任何简单/内置的方式来使MVC3的小写URL。
我有代码可以做到这一点,但看起来很简单的实施。
routes.MapRouteLowercase( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "index", id = UrlParameter.Optional } // Parameter defaults ); public class LowercaseRoute : Route { public LowercaseRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) { } public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { } public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) { } public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) { } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { VirtualPathData path = base.GetVirtualPath(requestContext, values); if (path != null) { path.VirtualPath = path.VirtualPath.ToLowerInvariant(); } return path; } } public static class RouteCollectionExtensions { public static Route MapRouteLowercase(this RouteCollection routes, string name, string url) { return routes.MapRouteLowercase(name, url, null /* defaults */, (object)null /* constraints */); } public static Route MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults) { return routes.MapRouteLowercase(name, url, defaults, (object)null /* constraints */); } public static Route MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, object constraints) { return routes.MapRouteLowercase(name, url, defaults, constraints, null /* namespaces */); } public static Route MapRouteLowercase(this RouteCollection routes, string name, string url, string[] namespaces) { return routes.MapRouteLowercase(name, url, null /* defaults */, null /* constraints */, namespaces); } public static Route MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, string[] namespaces) { return routes.MapRouteLowercase(name, url, defaults, null /* constraints */, namespaces); } public static Route MapRouteLowercase(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"); } Route route = new LowercaseRoute(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints), DataTokens = new RouteValueDictionary() }; if ((namespaces != null) && (namespaces.Length > 0)) { route.DataTokens["Namespaces"] = namespaces; } routes.Add(name, route); return route; } public static Route MapRouteLowercase(this AreaRegistrationContext context, string name, string url) { return context.MapRouteLowercase(name, url, (object)null /* defaults */); } public static Route MapRouteLowercase(this AreaRegistrationContext context, string name, string url, object defaults) { return context.MapRouteLowercase(name, url, defaults, (object)null /* constraints */); } public static Route MapRouteLowercase(this AreaRegistrationContext context, string name, string url, object defaults, object constraints) { return context.MapRouteLowercase(name, url, defaults, constraints, null /* namespaces */); } public static Route MapRouteLowercase(this AreaRegistrationContext context, string name, string url, string[] namespaces) { return context.MapRouteLowercase(name, url, (object)null /* defaults */, namespaces); } public static Route MapRouteLowercase(this AreaRegistrationContext context, string name, string url, object defaults, string[] namespaces) { return context.MapRouteLowercase(name, url, defaults, null /* constraints */, namespaces); } public static Route MapRouteLowercase(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.MapRouteLowercase(name, url, defaults, constraints, namespaces); route.DataTokens["area"] = context.AreaName; // disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up // controllers belonging to other areas bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0); route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback; return route; } }
我刚刚注意到在.NET Framework 4.5中有一个新属性。 太棒了! RouteCollection.LowercaseUrls
将LowercaseUrls设置为true
public static void RegisterRoutes(RouteCollection routes) { routes.LowercaseUrls = true; ... }
创build一个链接
@Html.ActionLink("Log in", "Login", "Account")
这将创build真棒,小写的url:)
<a href="/account/login">Log in</a>
这里有一个NuGet包: LowerCaseRoutesMVC ( 项目网站 )
如果您的强制执行小写字母的理由纯粹是SEO,那么我发现的最佳解决scheme是使用IIS 7 URL重写模块
您不仅可以强制所有url的小写,但您也有权访问规则,允许您删除/添加尾部的斜线,强制规范域等。
RuslanY的博客有一个好的开始。 例如,我在我的所有网站上使用以下内容:
<!-- http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/ --> <rule name="Convert to lower case" stopProcessing="true"> <match url=".*[AZ].*" ignoreCase="false" /> <conditions> <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="GET" ignoreCase="false" /> </conditions> <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" /> </rule>
您只需将上述几行添加到web.config的部分即可。
是的,我必须执行类似于上述的东西。 这似乎是实现这一目标的唯一顺利方法。
我想补充一点,我们还增加了301个redirect,这样任何来自/大写Url的stream量都会被301redirect到/ upper-case-url。
不知道你的代码有什么问题(我有点不同,但基本上是相同的概念)。 这是非常容易实现的,它是完全可重用的。
LowerCaseRouteHelper.cs
using System.Web.Routing; using System.Web.Mvc; namespace Utilities.Helpers { public class LowercaseRouteHelper : System.Web.Routing.Route { public LowercaseRouteHelper(string url, IRouteHandler routeHandler) : base(url, routeHandler) { } public LowercaseRouteHelper(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { } public LowercaseRouteHelper(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) { } public LowercaseRouteHelper(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) { } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { VirtualPathData path = base.GetVirtualPath(requestContext, values); if (path != null) { path.VirtualPath = path.VirtualPath.ToLowerInvariant(); } return path; } } public static class RouteCollectionExtensions { [System.Runtime.CompilerServices.Extension()] public static void MapRouteLowercase(RouteCollection routes, string name, string url, object defaults) { routes.MapRouteLowercase(name, url, defaults, null); } [System.Runtime.CompilerServices.Extension()] public static void MapRouteLowercase(RouteCollection routes, string name, string url, object defaults, object constraints) { if (routes == null) { throw new ArgumentNullException("routes"); } if (url == null) { throw new ArgumentNullException("url"); } object route = new LowercaseRouteHelper(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints) }; if (String.IsNullOrEmpty(name)) { routes.Add(route); } else { routes.Add(name, route); } } } }
全球
routes.MapRouteLowercase("Start", "", new { controller = "Home", action = "Index" })
我喜欢这个,而伟大的事情(正如评论所说)是真的有助于改善search引擎优化。
大声笑你们这样做太难了。 只需将其添加到您的global.asax application_beginrequest中即可
protected void Application_BeginRequest(Object sender, EventArgs e) { string url = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath); if (Regex.IsMatch(url, @"[AZ]")) { PermanentRedirect(url.ToLower() + HttpContext.Current.Request.Url.Query); } private void PermanentRedirect(string url) { Response.Clear(); Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", url); Response.End(); }