我現在已經找到了解決我原來的問題。我發現了下面的文章,它描述瞭如何確保所有Url都是小寫的,所以我只是使用了這個示例代碼,但在它執行ToLowerInvariant()的地方附加了一個尾部的斜線。
所以,我的2個輔助類現在看起來是這樣的:
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的,而不是我的RegisterRoutes內使用「圖路線」套路,我打電話給我的新MapRouteLowercase方法,而不是(傳入相同的參數),例如
routes.MapRouteLowercase("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
,我得到了可以在這裏找到這段代碼的原創文章:
http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/
在發佈問題和答案之間,您是否偶然從asp.net切換到mvc? routes.MapRouteLowercase(...)顯然適用於mvc控制器/操作。 – secretwep 2016-04-12 20:53:44