get the page name from url

本文介绍了一种在ASP.NET中高效获取当前执行的aspx页面名称的方法,避免了使用FileInfo对象或访问文件系统的复杂性。通过Page对象的AppRelativeVirtualPath属性,可以轻松获得页面名称,即使在URL重写的情况下也能正常工作。

https://stackoverflow.com/questions/1874532/better-way-to-get-page-name

The way I interpret the question what you're looking for is an efficient way of retrieving the name of the currently executing aspx page, ie System.Web.UI.Page.

If that is true you shouldn't have to deal with any FileInfo objects or hit the filesytem. Simply use the AppRelativeVirtualPath property on the page object.

using System; using System.IO; using System.Web.UI; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string pageName = Path.GetFileNameWithoutExtension(Page.AppRelativeVirtualPath); } } }

If you wan't to get the fully qualified (or "rooted") path of your currently executing page you can use Server.MapPath like this

string path = Server.MapPath(Page.AppRelativeVirtualPath); //这里的路径是网页对应的物理文件的路径

Using AppRelativeVirtualPath has the benefit of working even when you're using url rewriting and since it doesn't use Request.Url (which is provided by your users) you don't have to worry about potentially malicious data.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值