소문자 경로 (기본적으로 경로를 대소 문자를 구분하지 않음)를 지원하려는 경우 아래에서 체크 아웃 할 수 있습니다. 우리는 현재 이것을 사용하고 있으며 훌륭하게 작동합니다. 그런 다음
using System;
using System.Web.Mvc;
using System.Web.Routing;
namespace MyMvcApplication.App.Helpers
{
public class LowercaseRoute : System.Web.Routing.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 void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults)
{
routes.MapRouteLowercase(name, url, defaults, null);
}
public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, object constraints)
{
if (routes == null)
throw new ArgumentNullException("routes");
if (url == null)
throw new ArgumentNullException("url");
var route = new LowercaseRoute(url, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(defaults),
Constraints = new RouteValueDictionary(constraints)
};
if (String.IsNullOrEmpty(name))
routes.Add(route);
else
routes.Add(name, route);
}
}
}
당신의 Global.asax에서 사용 참조 :
은 첫째로 당신은 당신이 (ASP.NET MVC RC1에서와 같은 호환) 다음으로 마음에 드는 RouteExtensions.cs 파일, 또는 명명 된 것도 필요합니다. cs 파일을 위 클래스에 추가하면 소문자 경로를 만들 수 있습니다. 아래의 소문자 경로를 볼 수 있으며이 경로를 호출 할 때마다 URL이 소문자로 바뀝니다.
routes.MapRouteLowercase(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "index", id = ""} // Parameter defaults
);
당신은 들어오는 URL의 당신이) (당신의 Application_BeginRequest에 방법 (기억을이 사용할 수 있습니다 (수동으로 링크를 사용자가 입력 또는 전화) 소문자로는, 이것은 소문자 루트 필요하지 않습니다 변환에 관심이 선택적 경우 자신은 위의 코드는) 그 처리됩니다
protected void Application_BeginRequest(Object sender, EventArgs e)
{
// If upper case letters are found in the URL, redirect to lower case URL.
// Was receiving undesirable results here as my QueryString was also being converted to lowercase.
// You may want this, but I did not.
//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();
//}
// If upper case letters are found in the URL, redirect to lower case URL (keep querystring the same).
string lowercaseURL = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath);
if (Regex.IsMatch(lowercaseURL, @"[A-Z]"))
{
lowercaseURL = lowercaseURL.ToLower() + HttpContext.Current.Request.Url.Query;
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", lowercaseURL);
Response.End();
}
}
대답은 원래 SO 게시 this에서왔다.
이 답변의 내용은 더 이상 사용할 수 없지만 archive.org here에서 볼 수있는 블로그 게시물에서 가져 왔습니다.