How to rewrite URL in an ASP.net site

橙三吉。 提交于 2019-12-06 04:39:25

问题


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>


来源:https://stackoverflow.com/questions/22756107/how-to-rewrite-url-in-an-asp-net-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!