默认情况下,我无法将路由设置为小写。由于某些原因,它不起作用。我知道我可以将authorize
和home
设置为小写,但是Admin
部分(区域)仍会大写。@Html.ActionLink("Hello World", "Authorize", "Home")
输出到<a href="/Admin/Home/Authorize">Hello World</a>
区域路线
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.LowercaseUrls = true;
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "OR.Areas.Admin.Controllers" }
);
context.Routes.LowercaseUrls = true;
}
默认路线
public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Localization",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "OR.Controllers" }
);
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "OR.Controllers" }
);
routes.LowercaseUrls = true;
}
我尝试过的管理区域配置
// admin/Home/Authorize
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.LowercaseUrls = true;
context.MapRoute(
"Admin_default",
"{area}/{controller}/{action}/{id}",
new { area = "admin", controller = "home", action = "Index", id = UrlParameter.Optional },
new string[] { "ORMebeles.Areas.Admin.Controllers" }
);
context.Routes.LowercaseUrls = true;
}
// admin/Home/Authorize
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.LowercaseUrls = true;
context.MapRoute(
"Admin_default",
"admin/{controller}/{action}/{id}",
new { controller = "home", action = "Index", id = UrlParameter.Optional },
new string[] { "ORMebeles.Areas.Admin.Controllers" }
);
context.Routes.LowercaseUrls = true;
}
编辑
看来这是MVC4的错误-当您设置
context.Routes.LowercaseUrls = true;
并且您拥有Area/Areas时context.Routes.LowercaseUrls = true;
不会生效,我们应该在哪里报告或如何修复它? 最佳答案
这是与MVC4相关的错误,将在MVC5版本中修复。 Routes.LowercaseUrls
不影响区域。更多信息here。
同时,您可以使用LowercaseRoutesMVC或
LowercaseRoutesMVC4(如果需要WebApi支持)。
关于c# - “routes.LowercaseUrls = true;”不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12699426/