Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab .aspx Page Name from the URL?

Tags:

asp.net

What object can I use to get the current PageName.aspx (including the extension .aspx) from the URL? I can't find what object and method to allow me to grab this when I'm on a page.

like image 252
PositiveGuy Avatar asked Dec 02 '09 14:12

PositiveGuy


People also ask

How to get current Page Name from URL in asp net?

AbsolutePath; string fileName = System. IO. Path. GetFileName(fullPath);

How do I create a link in ASPX?

To create HyperLink either we can write code or use the drag and drop facility of visual studio IDE. This control is listed in the toolbox. This is a server side control and ASP.NET provides own tag to create it. The example is given below.


6 Answers

Note that sometimes, on shared hosting like GoDaddy, you might not have the permission to create a new FileInfo object. Yes, believe it.

So I suggest you use this snippet:

string fullPath = /* System.Web.HttpContext.Current. (optional in most cases) */ Request.Url.AbsolutePath;
string fileName = System.IO.Path.GetFileName(fullPath);

Enjoy :-)

like image 179
Ron Klein Avatar answered Sep 29 '22 09:09

Ron Klein


Pino here's the source lil man: http://www.devx.com/tips/Tip/42433

  string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath);
    string sPageName = oFileInfo.Name;
like image 45
JonH Avatar answered Sep 29 '22 10:09

JonH


http://www.aspcode.net/Get-current-page-name.aspx

public string GetCurrentPageName() 
{ 
    string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath; 
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath); 
    string sRet = oInfo.Name; 
    return sRet; 
} 
like image 30
LiamB Avatar answered Sep 29 '22 09:09

LiamB


Request.Url.AbsolutePath

Split about '/', last item is your file name.

like image 28
Alex Polkhovsky Avatar answered Sep 29 '22 10:09

Alex Polkhovsky


Path.GetFileName(Request.PhysicalPath) can be used to fetch the actual file name

like image 44
prameela rani Avatar answered Sep 29 '22 09:09

prameela rani


I used: Request.Url.LocalPath, which gave me "/default.aspx", even when full URL would be https://www.example.com/?foo=bar. You just need to strip the leading /.

like image 28
Adam Avatar answered Sep 29 '22 08:09

Adam