get the page name from 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.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(223)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2017-09-26 IIS访问站点,出现connection refused
2016-09-26 Entity Framework 与多线程
2015-09-26 使用git批量删除分支
点击右上角即可分享
微信分享提示