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.
AbsolutePath; string fileName = System. IO. Path. GetFileName(fullPath);
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.
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 :-)
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;
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;
}
Request.Url.AbsolutePath
Split about '/', last item is your file name.
Path.GetFileName(Request.PhysicalPath) can be used to fetch the actual file name
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 /
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With