.NET 4.5 MVC RouteCollection.LowercaseUrls breaks when using Area

醉酒当歌 提交于 2020-01-10 17:29:14

问题


A new property to RouteCollection was added with .NET Framework 4.5:

http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.lowercaseurls.aspx

This works great, until you add an Area to your project. All Urls are propercase again.

Any ideas why this happens? It's simple to reproduce:

  1. Create new MVC 4 Internet Application (.NET 4.5)
  2. Start RouteConfig.RegisterRoutes function with: routes.LowercaseUrls = true;
  3. Run the project and you'll see all generated Urls are now lowercase.
  4. Add an Area to the project.
  5. Run the project again and you'll see all Urls are propercase again!

It breaks at the point where context.MapRoute gets executed in AreaRegistration.RegisterArea. When I comment that out, I got lowercase Urls. Uncomment it, and the propercase Urls are back.

It seems to me like Microsoft knows this is buggy and doesn't advertise this new feature, as it's a really great feature but it is only mentioned in the MSDN docs.


回答1:


This indeed appears to be a bug in the interaction between ASP.NET MVC and ASP.NET 4.5's new LowercaseUrls feature in routing.

I have logged a bug for MVC here: http://aspnetwebstack.codeplex.com/workitem/685

Thank you for reporting this!

As Cristi Pufu mentions, it appears that others have written some NuGet packages to handle similar scenarios:

  • http://nuget.org/packages/LowercaseRoutesMVC - adds support for lowercase URLs
  • http://nuget.org/packages/AttributeRouting - adds super fancy support for tons of neat feature, including lowercase URLs (see https://github.com/mccalltd/AttributeRouting/wiki/Generating-Lowercase-Outbound-URLs)

EDIT 4/23/2013 - We have recently accepted a pull request with a fix for this issue for ASP.NET MVC 5.



来源:https://stackoverflow.com/questions/13271048/net-4-5-mvc-routecollection-lowercaseurls-breaks-when-using-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!