How to display URL in lower case?

2019-07-23 00:15发布

I have a webforms projects with uppercase URLs being returned to the client:

http://www.looknbook.com/Packages/Forms/package_search.aspx

The requirement is to have the URLs display in lowercase:

http://www.looknbook.com/packages/forms/package_search.aspx

How do I send URLs to the client's browser in lowercase in ASP.NET Webforms?

3条回答
再贱就再见
2楼-- · 2019-07-23 00:37

Classic ASP .NET paths correspond to folder names, and folders are not case-senstive in Windows.
Thefefore, both lowercase and capitalized URLs can be used to access your site.

The only reason you see an capitalized URL in the address bar is because the links have it capitalized. Change all links on the site to be lowercase, and that's it.

If you also want to force lowercase (i.e. change to lowercase even if the user entered a capitalized URL), you'll need to do URL rewriting but concrete solutions depend on the version of IIS you're using.

查看更多
Emotional °昔
3楼-- · 2019-07-23 00:49

For IIS7, you can use URI Rewrite and use the example here:

<rule name="Convert to lowercase" stopProcessing="true">  
    <match url=".*[A-Z].*" ignoreCase="false" />  
    <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
查看更多
仙女界的扛把子
4楼-- · 2019-07-23 00:51

if you want to achieve lower case URL from code site than we can achieve that by implementing following code in Application_BeginRequest or Application_EndRequest of global.cs page

            var curenturl = Request.Url.ToString();           
            if (Regex.IsMatch(curenturl, @"[A-Z]"))
            {
                Response.Clear();
                Response.Status = "301 Moved Permanently";
                Response.StatusCode = 301;
                Response.AddHeader("Location", curenturl.ToLower());
                Response.End();
            }
查看更多
登录 后发表回答