How to rewrite URL in an ASP.net site

2019-04-10 12:18发布

问题:

I want to rewrite URL in an asp.net site

What i need is i don't want the user to see in which language the site was created

i.e it should not have www.examplesite.com/index.aspx as address

instead i want it as www.examplesite.com/index

I don't want the user to see the extension of files

If this question is not related to Stackoverflow please redirect this question to the respective site of Stack Exchange.

Any help is appreciated.

回答1:

You can do this at a simple level in the Global.asax file like this:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request

    Dim path As String = HttpContext.Current.Request.Path

    If path.ToLower.EndsWith(".aspx") Then
        path = path.Substring(0, path.Length - 5)
        Response.Redirect(path, True)
    Else
        path += ".aspx"
        Context.RewritePath(path)
    End If

End Sub

If you have other files that are requested such as .png files, you may need some additional logic to filter these out.



回答2:

The issue related with your question is the URL Rewriting in ASP.Net. For URL rewriting there are various approaches: writing rewrite module in IIS or using ASP.NET Web.config file.

For using web.config file, you've to first of all add rewrite configuration section and the define the rewrite rule as per your requirement within the <rewrite > </rewrite> tags.

For more details: follow this Link.

I hope this will help you.



回答3:

In web.config file, add below code.

<rewrite>
       <rules>
            <clear />

                <rule name="exampleredirect" stopProcessing="true">
                    <match url="^index.aspx" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="/index" />
                </rule>
        </rules>
</rewrite>

Please refer the below link.

http://forums.asp.net/t/1910607.aspx?web+config+rewrite+rule



回答4:

There is a nuget package for this I think, its called Friendly Urls



回答5:

In the web.config you'd have to add a section called <rewrite> </rewrite>, then you would add rules/rule names as I've done below:

            <rewrite><rules><clear />
               <rule name="redirectrule" stopProcessing="true">
               <match url="^index.aspx" />
               <conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
               <action type="Redirect" url="/index" />
            </rule></rules></rewrite>