如何确保在POST小写的网址吗? [英] How do I ensure lower case URLs on POST?

查看:151
本文介绍了如何确保在POST小写的网址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保用于访问我的ASP.NET MVC网站的所有网址都小写。在这种大写字母是在URL的情况下,我改变了状态code至301和改变位置的URL的小写版本与此code:

I am trying to ensure that all URLs used to access my ASP.NET MVC site are lower case. In the event that an upper case letter is in the URL, I'm changing the status code to 301 and changing the location to the lowercase version of the URL with this code:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var url = Request.Url.ToString();
    if (Regex.IsMatch(url, @"[A-Z]"))
    {
       Response.Clear();
       Response.Status = "301 Moved Permanently";
       Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
       Response.AddHeader("Location", url.ToLower());
       Response.End();
     }
}

不过,最近一位同事试图张贴形式以大写字母(忘记了重定向)的URL,但动作(标有HttpPost属性)没有被击中。看着萤火虫的要求显示了原来的职位,但随后返回301并发出GET以小写版本。

However, recently a co-worker was trying to POST a form to a URL with a capital letter (forgetting about the redirect), but the action (marked with an HttpPost attribute) was not being hit. Looking at the requests in Firebug showed the original POST, but then it returned 301 and issued a GET to the lower case version.

我猜最好的解决方法就是确保所有的职位是对URL的小写版本,但我来到这里,看看是否有另一种方式来处理这个问题。

I'm guessing the best solution is to just make sure all POSTs are to the lowercase version of the URL, but I came here to see if there is another way to handle this issue

推荐答案

下面是一个基于scottm的解决方案的一个版本,与.NET 4.0的作品

Here's a version based on scottm's solution that works with .NET 4.0

protected void Application_BeginRequest(object sender, EventArgs e) {
    string url = Request.Url.ToString();
    if (Request.HttpMethod == "GET" && Regex.Match(url, "[A-Z]").Success) {
        Response.RedirectPermanent(url.ToLower(), true);
    }
}

这篇关于如何确保在POST小写的网址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆