Reputation: 47743
I'm using this to get the current page name..so this returns for example MyPage.aspx
public string GetCurrentPageName()
{
string urlPath = Request.Url.AbsolutePath;
FileInfo fileInfo = new FileInfo(urlPath);
string pageName = fileInfo.Name;
return pageName;
}
There has to be an easier way? Meaning there's got to be an existing method or property in the .NET framework one would think.
Upvotes: 35
Views: 65805
Reputation: 63
Dim MyPage As String = Path.GetFileName(Page.AppRelativeVirtualPath.ToString).ToString
This one works for me
Upvotes: 2
Reputation: 22580
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, i.e. System.Web.UI.Page.
If that is true you shouldn't have to deal with any FileInfo objects or hit the filesystem. 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 want 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.
Upvotes: 59
Reputation: 33098
Would HttpContext.Current.CurrentHandler
be what you're looking for?
Since you're more interested in the physical file name vs the page object something along the lines of
var page = (Page) HttpContext.Current.CurrentHandler;
string url = page.AppRelativeVirtualPath;
This along with the information from @Markus Olsson can give you access to the page in any point during its execution even if you're outside of page class.
Upvotes: 11
Reputation: 8007
I use Request.Url.Segments.Last()
, which I think is elegant enough.
Upvotes: 10
Reputation: 26956
As was pointed out in one of the answers to this earlier question of yours, I'd go for an option that didn't need me to create a FileInfo
object.
There isn't always a direct mapping between a requested page and a file system object, especially when routing/url rewriting etc comes in to play.
Upvotes: 1
Reputation: 8474
just for interest I did little search with intellisence. did not found any property. still same logic in other way round.
string currentPageName = System.IO.Path.GetFileName(Request.Url.AbsolutePath);
Upvotes: 7
Reputation: 754488
Not much better, but you could try this extension method:
public static string GetPageName(this Page myPage)
{
FileInfo fi =new FileInfo(myPage.MapPath(myPage.AppRelativeVirtualPath));
return fi.Name;
}
and just call it in your page's "OnInit" or whatever method as:
string pageName = this.GetPageName();
Marc
Upvotes: 0