How to display URL in lower case?

2019-07-23 00:09发布

问题:

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?

回答1:

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>


回答2:

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.



回答3:

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();
            }