如何在 ASP.net 站点中重写 URL [英] How to rewrite URL in an ASP.net site

查看:22
本文介绍了如何在 ASP.net 站点中重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写一个 asp.net 站点中的 URL

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

即它不应该有 www.examplesite.com/index.aspx 作为地址

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

相反,我希望它是 www.examplesite.com/index

我不想让用户看到文件的扩展名

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

如果此问题与 Stackoverflow 无关,请将此问题重定向到 Stack Exchange 的相应站点.

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

感谢任何帮助.

推荐答案

您可以在 Global.asax 文件中的简单级别执行此操作,如下所示:

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

如果您有其他需要的文件,例如 .png 文件,您可能需要一些额外的逻辑来过滤掉这些文件.

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

这篇关于如何在 ASP.net 站点中重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆