• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

LowercaseRoutesMVCASP.NETMVCroutestolowercaseURLs

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Tired of your MVC application generating mixed-case URLs like http://mysite.com/Home/About orhttp://mysite.com/Admin/Customers/List? Wouldn't http://mysite.com/home/about andhttp://mysite.com/admin/customers/list be a lot nicer? I think so too, so I wrote this little project to help make that work.

By default, ASP.NET MVC generates outgoing route URLs that preserve the casing of your controller and action names. Since controllers are classes and actions are methods, these outgoing URL segments will most likely be camel-cased, as that is the standard when naming these constructs. LowercaseRoutesMVC lets you very easily make ASP.NET MVC generate outgoing route URLs that are always lower-cased, without having to rename any of your controller classes or action methods. It includes support for MVC Areas as well. (If the generated outgoing URL contains a querystring, its casing will not be affected.)

There is also a separate beta version with support for lower-casing HTTP Routes generated by the ASP.NET Web API.

Usage Notes

Using this project is very easy. Once added as a reference in your web project, you need only make a minor change to the method calls in your RegisterRoutes method in Global.asax. If you are using Areas in your project, you will also make similar changes in the RegisterArea override(s) in your area registration classes

Step 1. - Install the library via Nuget

PM> Install-Package LowercaseRoutesMVC or via the NuGet management tools in Visual Studio.

If you need support for lower-casing HTTP Routes, install this version instead:

PM> Install-Package LowercaseRoutesMVC4

Step 2. - Remap Your Main Routes

Go to the RegisterRoutes method in Global.asax. Replace all the calls to MapRoute withMapRouteLowercase.

For example:

using LowercaseRoutesMVC
 ...

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRouteLowercase( // changed from routes.MapRoute
       "Default",
       "{controller}/{action}/{id}",
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

  

Step 3. (if using Areas) - Remap Your Area Routes

Go to the RegisterArea override in the area registration class for each Area in your application. (For example, if you have an Area named Admin, this class will be called AdminAreaRegistration and will be located in theAreas/Admin folder.) Again, replace all the calls to MapRoute with MapRouteLowercase.

For example:

using LowercaseRoutesMVC
 ...

public override void RegisterArea(AreaRegistrationContext context)
{
   context.MapRouteLowercase( // changed from context.MapRoute
       "Admin_default",
       "Admin/{controller}/{action}/{id}",
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

  

Step 4. (if using ASP.NET Web API) - Remap Your HTTP Routes

There is a separate Nuget package called LowercaseRoutesMVC4 that contains all of the functionality above, plus beta support for lower-casing HTTP Routes generated by the ASP.NET Web API. If this is something you're interested in doing, be sure to install the LowercaseRoutesMVC4 Nuget package and not the LowercaseRoutesMVC one. Keep in mind that the ASP.NET Web API is still in beta, and that the routing features are likely to change.

Go to the RegisterRoutes method in Global.asax. Replace all the calls to MapHttpRoute withMapHttpRouteLowercase.

For example:

using LowercaseRoutesMVC4
 ...

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapHttpRouteLowercase( // changed from routes.MapHttpRoute
       "DefaultAPI",
       "api/{controller}/{id}",
       new { id = UrlParameter.Optional }
   );
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETCore环境变量和启动设置的配置教程发布时间:2022-07-10
下一篇:
收藏一篇关于Asp.net Response.Filter的文章发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap