zoukankan      html  css  js  c++  java
  • LowercaseRoutesMVC ASP.NET MVC routes to lowercase URLs

    About this Project

    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 }
       );
    }
    

      

  • 相关阅读:
    .NET Remoting 应用实例
    EXT.NET 使用总结(3)--动态图表
    2013,2014
    TreeMap put 操作分析
    C#排序算法小结
    高性能的JavaScript--数据访问(1)
    javascript生成对象的三种方法
    Android开发中经常使用的Content-Type简介
    git diff提示filemode发生改变(old mode 100644、new mode 10075)
    UIWebView的使用
  • 原文地址:https://www.cnblogs.com/alphafly/p/5125920.html
Copyright © 2011-2022 走看看