在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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. Usage NotesUsing 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 NugetPM> 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 RoutesGo 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 RoutesGo 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 RoutesThere 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. 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 } ); }
|
请发表评论