在MVC5中使用属性路由来简化操作

问题描述

我正在MVC5应用程序中利用属性路由,并且喜欢能够按照以下示例在控制器类级别上声明约定。对于控制器内的操作,是否存在建议的方式来强制使用小写的路径/ URL约定?我目前在自定义GetAreaPrefix中覆盖了DefaultDirectRouteProvider,以强制使用小写的区域前缀,但是在操作上强制使用小写的约定而又不必在每个路由上都放置route属性,是否有等效的方法

namespace TestNamespace
{
    [RouteArea("TestArea",AreaPrefix = "testarea/testpath")]
    [Route("{action=index}")]
    public class TestController : Controller
    {
        /// <summary>
        /// this returns /testarea/testpath/Index (note the action is not lowercase)
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// this returns a properly lowercased path of /testarea/testpath/details
        /// Do we really need to put route attributes on each action? Is it possible to have a custom declaration at the class level?
        /// </summary>
        /// <returns></returns>
        [Route("details")]
        public ActionResult Details()
        {
            return View();
        }
    }
}

解决方法

经过进一步研究,在global.asax的RegisterRoutes中将路由集合LowercaseUrls属性设置为true对我有用:

routes.LowercaseUrls = true;