在ASP.NET MVC强制区分大小写的路由 [英] Force case-sensitive routing in ASP.NET MVC
问题描述
这个问题已经被问了相似但不完全相同的方式(而不是解决让我满意)<一个href=\"http://stackoverflow.com/questions/878578/how-can-i-have-lowercase-routes-in-asp-net-mvc\">$p$pviously在<一个href=\"http://stackoverflow.com/questions/170900/how-can-i-avoid-duplicate-content-in-asp-net-mvc-due-to-case-insensitive-urls-an\">Stack溢出和别处。
This question has been asked in a similar but not identical fashion (and not resolved to my satisfaction) previously on Stack Overflow and elsewhere.
从Linux世界的到来,我想用ASP.NET MVC但避免从解决到同一页面相同,但不同的-套管路线。我的不的要强制所有路由100%小写。
Coming from a linux-world, I want to use ASP.NET MVC but avoid identical but differently-cased routes from resolving to the same page. I do not want to force all routes to be 100% lowercase.
例如。我想 /主页/东西
是一个有效的途径和 /主页/ somethingElse
来也算是一个有效的途径,但没有 /主页/东西
或 /家庭/ somethingelse
,给出了两个函数,叫的东西
和 somethingElse
在的HomeController
。
e.g. I want /Home/Something
to be a valid route and /Home/somethingElse
to also be a valid route, but not /Home/something
or /home/somethingelse
, given two functions called Something
and somethingElse
in the HomeController
.
我找不到从的RegisterRoutes
函数中这样做的任何方式,但也许我失去了一些东西明显?我可以通过添加code到每个控制器功能回答这个问题很轻松了,但我显然试图避免这样做。
I can't find any way of doing this from within the RegisterRoutes
function, but maybe I'm missing something obvious? I can answer this easily enough by adding code to each Controller function, but I'm obviously trying to avoid doing that.
最佳地,该解决方案将涉及捕获的所有的特定路线的排列,然后301重定向任何不准确控制器的功能的情况相匹配。
Optimally, the solution would involve catching all permutations of a particular route, then 301 redirecting any that do not exactly match the case of the controller's function.
推荐答案
我无法找到经过广泛的搜索这样做的任何的方式。基本上,区分大小写和IIS / ASP.NET显然不一起去。
I was unable to find any way of doing this after extensive searching. Basically, case-sensitivity and IIS/ASP.NET apparently do not go together.
我们现在正在使用有点杂牌组装电脑来解决这个问题。在code已被opensourced(MIT许可)在GitHub上: NeoSmart Web工具包,特别是< A HREF =https://github.com/NeoSmart/web/blob/master/Web%20Toolkit/Seo.cs相对=nofollow>包含SEO重定向code 此文件。
We're now using a bit of a kludge to solve this. The code has been opensourced (MIT license) on github: NeoSmart Web Toolkit, in particular, this file containing the SEO redirect code.
使用它是很容易:控制器类中的每个GET方法需要在开始时只添加这一行:
Using it is easy enough: each GET method in the controller classes needs to add just this one line at the start:
Seo.SeoRedirect(this);
搜索引擎优化重写类自动使用C#5.0的来电信息属性做繁重,使得code以上严格的复制和粘贴。
The SEO rewrite class automatically uses C# 5.0's Caller Info attributes to do the heavy lifting, making the code above strictly copy-and-paste.
在理想情况下,我很想找到一个办法把该行code成一个属性。例如,prefixing与控制器方法 [的CaseSensitive]
将自动拥有写作在该行同样的效果,但很可惜,我没有(还)知道如何要做到这一点。
Ideally, I would love to find a way to turn that line of code into an attribute. For instance, prefixing the controller methods with [CaseSensitive]
would automatically have the same effect as writing in that line, but alas, I do not (yet) know how to do this.
我也找不到与路由
类/结构搞清楚了这一点的任何方式。这是一些不透明code!
I also can't find any way of figuring this out with the Routing
class/structures. That's some opaque code!
这篇关于在ASP.NET MVC强制区分大小写的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!