我也在研究这个问题.我显然会顺从ScottGu.我谦卑地提供了解决这个问题的方法.
将以下代码添加到global.asax:
protected void Application_BeginRequest(Object sender, EventArgs e) { // If upper case letters are found in the URL, redirect to lower case URL. if (Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), @"[A-Z]") == true) { string LowercaseURL = HttpContext.Current.Request.Url.ToString().ToLower(); Response.Clear(); Response.Status = "301 Moved Permanently"; Response.AddHeader("Location",LowercaseURL); Response.End(); } }
一个很好的问题!
我也在研究这个问题.我显然会顺从ScottGu.我谦卑地提供了解决这个问题的方法.
将以下代码添加到global.asax:
protected void Application_BeginRequest(Object sender, EventArgs e) { // If upper case letters are found in the URL, redirect to lower case URL. if (Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), @"[A-Z]") == true) { string LowercaseURL = HttpContext.Current.Request.Url.ToString().ToLower(); Response.Clear(); Response.Status = "301 Moved Permanently"; Response.AddHeader("Location",LowercaseURL); Response.End(); } }
一个很好的问题!
除了在这里发帖,我还通过电子邮件向ScottGu发送电子邮件,看他是否有好的回复.他给出了一个为路由添加约束的示例,因此您只能响应小写网址:
public class LowercaseConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { string value = (string)values[parameterName]; return Equals(value, value.ToLower()); }
并且在寄存器路由方法中:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "home", action = "index", id = "" }, new { controller = new LowercaseConstraint(), action = new LowercaseConstraint() } ); }
这是一个开始,但是我希望能够从Html.ActionLink和RedirectToAction等方法更改链接的生成以匹配.