在ASP中强制区分大小写的路由。NET MVC

时间:2022-10-16 07:20:40

This question has been asked in a similar but not identical fashion (and not resolved to my satisfaction) previously on Stack Overflow and elsewhere.

这个问题在之前的Stack Overflow和其他文章中都有过类似但不完全相同的问题。

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.

来自linux世界,我想使用ASP。但是要避免从解析到同一页面的相同但不同的方法。我不想强制所有的路径都是100%的小写。

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.

例如:I want /Home/Something to be a valid route and /Home/Something else to be a valid route, but not /Home/Something or /Home/somethingElse, given two function called Something and Something else in the HomeController。

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.

我在registerroute函数中找不到这样做的方法,但我可能漏掉了一些明显的东西?通过向每个控制器函数添加代码,我可以很容易地回答这个问题,但显然我在尽量避免这样做。

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.

最优的解决方案是捕获特定路径的所有排列,然后301重定向任何与控制器功能不完全匹配的路径。

1 个解决方案

#1


2  

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.

在广泛的搜索之后,我找不到任何方法来做这件事。基本上,区分大小写和IIS / ASP。显然,网络不是一起去的。

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.

我们现在用了一些杂凑的方法来解决这个问题。该代码是github: NeoSmart Web Toolkit上的开源(MIT license),特别是这个包含SEO重定向代码的文件。

Using it is easy enough: each GET method in the controller classes needs to add just this one line at the start:

使用它非常简单:控制器类中的每个GET方法都需要在开始时只添加这一行:

Seo.SeoRedirect(this);

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.

SEO重写类自动使用c# 5.0的调用者信息属性来完成繁重的工作,使上面的代码严格地复制粘贴。

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.

理想情况下,我希望找到一种方法,将这行代码转换成一个属性。例如,在controller方法前面加上[casesen]将自动产生与在该行中写入相同的效果,但是,唉,我(还)不知道如何做到这一点。

I also can't find any way of figuring this out with the Routing class/structures. That's some opaque code!

我也没办法用路由类/结构来解决这个问题。这是一些不透明的代码!

#1


2  

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.

在广泛的搜索之后,我找不到任何方法来做这件事。基本上,区分大小写和IIS / ASP。显然,网络不是一起去的。

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.

我们现在用了一些杂凑的方法来解决这个问题。该代码是github: NeoSmart Web Toolkit上的开源(MIT license),特别是这个包含SEO重定向代码的文件。

Using it is easy enough: each GET method in the controller classes needs to add just this one line at the start:

使用它非常简单:控制器类中的每个GET方法都需要在开始时只添加这一行:

Seo.SeoRedirect(this);

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.

SEO重写类自动使用c# 5.0的调用者信息属性来完成繁重的工作,使上面的代码严格地复制粘贴。

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.

理想情况下,我希望找到一种方法,将这行代码转换成一个属性。例如,在controller方法前面加上[casesen]将自动产生与在该行中写入相同的效果,但是,唉,我(还)不知道如何做到这一点。

I also can't find any way of figuring this out with the Routing class/structures. That's some opaque code!

我也没办法用路由类/结构来解决这个问题。这是一些不透明的代码!