如何解决在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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。