c# - Web 窗体项目中的小写 URL

标签 c# asp.net webforms

我正在 Visual Studio 2013 中开发 Web 表单应用程序,并希望将 URL 设为小写。

例如:

http://example.com/About

http://example.com/about

我找到的所有解决方案都是通过 IIS 重写规则,但我想在项目本身中解决它。

最佳答案

在 BeginRequest 上的 global.asax 上,您可以简单地进行检查,然后重定向为:

protected void Application_BeginRequest(Object sender, EventArgs e)     
{
    // place the lower case on string, to avoid make it again later.
    string cTheLowerUrl = HttpContext.Current.Request.Path.ToLowerInvariant();
    if (cTheLowerUrl != HttpContext.Current.Request.Path)
    {
        HttpContext.Current.Response.Redirect(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
    }
}

您还可以检查哪个文件强制执行该规则,例如仅检查 aspx 文件:

string sExtOfFile = System.IO.Path.GetExtension(HttpContext.Current.Request.Path);
if (sExtOfFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
{
    string cTheLowerUrl = HttpContext.Current.Request.Path.ToLowerInvariant();
    if (cTheLowerUrl != HttpContext.Current.Request.Path)
    {
        HttpContext.Current.Response.Redirect(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
    }
}

永久重定向

asp.net 4版本可以直接使用HttpResponse.RedirectPermanent 对于 asp.net 3.5 版本和之前,我使用 asp.net 进行类似的重定向,但使用 301 Moved Permanently 响应:

public static void RedirectPermanent(string url, bool endResponse = true)
{
    HttpResponse responce = HttpContext.Current.Response;

    #if DEBUG
    if (url == null)
    {
        throw new ArgumentNullException("url");
    }
    if (responce == null)
    {
        throw new ArgumentNullException("url");
    }
    if (url.IndexOf('\n') >= 0)
    {
        throw new ArgumentException("Cannot_redirect_to_newline");
    }

    Page handler = HttpContext.Current.Handler as Page;

    if ((handler != null) && handler.IsCallback)
    {
        throw new ApplicationException("Redirect_not_allowed_in_callback");
    }
    #endif

    url = responce.ApplyAppPathModifier(url);

    responce.Clear();
    responce.TrySkipIisCustomErrors = true;
    responce.StatusCode = 301;
    responce.Status = "301 Moved Permanently";
    responce.RedirectLocation = url;

    // a direct header diferent way 
    // responce.AddHeader("Location", url);     

    responce.Write("<html><head><title>Object moved</title></head><body>\r\n");
    responce.Write("<h2>Object moved to <a href=\"" + url + "\">here</a>.</h2>\r\n");
    responce.Write("</body></html>\r\n");

    if (endResponse)
    {
        responce.End();
    }
}

protected void Application_BeginRequest(Object sender, EventArgs e) 上的代码将是:

string sExtOfFile = System.IO.Path.GetExtension(HttpContext.Current.Request.Path);
if (sExtOfFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
{
    string cTheLowerUrl = HttpContext.Current.Request.Path.ToLowerInvariant();
    if (cTheLowerUrl != HttpContext.Current.Request.Path)
    {
            // for asp.net 4 and above
            HttpContext.Current.Response.RedirectPermanent(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
            // or using the above function.
            // RedirectPermanent(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
    }
}

评论

我首先对其进行了测试并且正在运行,比制定规则更快,并且您可以更直接地控制它。

关于c# - Web 窗体项目中的小写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20814899/

相关文章:

c# - 如何将 C# 连接到 Snowflake 数据库?

ASP.NET 页面事件 - GridView 绑定(bind)后出现按钮单击事件

webforms - 是否有等同于 Webforms 的 MvcBuildViews?

c# - 修改原始 DataSource 不会更新 ComboBox

c# - 如何将 ASP.NET MVC ViewResult 呈现为 HTML?

c# - Facebook API 错误 "Session key invalid or no longer valid"

javascript - 如何在表单提交前等待3秒?

c# - 在 asp.net webforms 中使用 http put 方法

c# - XElement.Root.Element 不断返回 null

c# - 请求查询从 vb 转换为 c#