ASP.NET 参数中的 MVC 小写路由
ASP.NET MVC lowercase routes in parameters
我有以下代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "CompanyDefault",
url: "Company",
defaults: new { controller = "Company", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
当我这样写时它工作正常:
@Url.Action("ApplicationDriverForm", "ApplicationDriver")
但是当我尝试使用参数时:
@Url.Action("ApplicationDriverForm", "ApplicationDriver", new { PaRaMeTeRwithUppercaseLetters = "BlaBlaBla" })
然后在这种情况下它生成 url 参数名称中的大写字母和参数值
如何让它在参数中也生成小写字母?
您匹配的路线(Default
路线)有一个名为 {id}
的值的段占位符
您的 @Url.Action()
代码正在为名为 PaRaMeTeRwithUppercaseLetters
的参数生成一个值,该值与您的路由不匹配,因此该值被添加为查询字符串,而不是路由值。
将生成 url 的代码更改为
@Url.Action("ApplicationDriverForm", "ApplicationDriver", new { id = "BlaBlaBla" })
或添加特定路由定义以匹配url(在默认路由之前)
routes.MapRoute(
name: "ApplicationDriver",
url: "ApplicationDriver/ApplicationDriverForm/{PaRaMeTeRwithUppercaseLetters}",
defaults: new { controller = "ApplicationDriver", action = "ApplicationDriverForm" }
);
我有以下代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "CompanyDefault",
url: "Company",
defaults: new { controller = "Company", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
当我这样写时它工作正常:
@Url.Action("ApplicationDriverForm", "ApplicationDriver")
但是当我尝试使用参数时:
@Url.Action("ApplicationDriverForm", "ApplicationDriver", new { PaRaMeTeRwithUppercaseLetters = "BlaBlaBla" })
然后在这种情况下它生成 url 参数名称中的大写字母和参数值
如何让它在参数中也生成小写字母?
您匹配的路线(Default
路线)有一个名为 {id}
您的 @Url.Action()
代码正在为名为 PaRaMeTeRwithUppercaseLetters
的参数生成一个值,该值与您的路由不匹配,因此该值被添加为查询字符串,而不是路由值。
将生成 url 的代码更改为
@Url.Action("ApplicationDriverForm", "ApplicationDriver", new { id = "BlaBlaBla" })
或添加特定路由定义以匹配url(在默认路由之前)
routes.MapRoute(
name: "ApplicationDriver",
url: "ApplicationDriver/ApplicationDriverForm/{PaRaMeTeRwithUppercaseLetters}",
defaults: new { controller = "ApplicationDriver", action = "ApplicationDriverForm" }
);