Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, August 25, 2003 11:50 AM
Hi, I am trying to get the current page file name that i access to. Lets say i am at test.aspx page, i want it to return me "test" file name. I tried to use this.Page.toString(), it returned me "ASP.test_aspx" which is like half correct.... but i would like to get only "test". 2nd question, this.Page.toString() will always return me in a format of ASP.[filename]_[fileextention]? If that's the case, maybe i can use splilt function to get the file name i want if there aren't any better solution than that. Thanks! regards, weihann.
All replies (10)
Monday, December 17, 2007 3:00 AM âś…Answered
Path.GetFileName(request.PhysicalPath);
It depends on what is the actual purpose - as many things can happen from the user request to the acual servicing of the request, you might want to use Request.PhysicalPath, you might also want to use the CurrentExecutionFilePath (this will take Server.Transfer etc into account).
You might also be interested in the actual request (after rewrite), and use Request.Url.AbsolutePath .
Finally (?) if you want the unmodified original use new Uri(Request.RawUrl).Absolute.Path.
In cases where a physical path is involved, use System.IO.Path.GetFileName() and System.IO.Path.GetExtension() methods to extract parts of it, and use System.Web.VirtualPathUtility.GetFileName() and GetExtension() methjods when it's a part of an URL or is a virtual path.
Why can't things just be simple? ;-)
Monday, August 25, 2003 1:44 PM
To save some parsing you could use Request.RawUrl, you will still need to parse out the extension. You might want to consider posting an argument to the page with the data you want - then you can easily query the page for that argument. HTH JD
Monday, August 25, 2003 8:05 PM
also you can use string pageName = this.Page.ToString().Substring(4,this.Page.ToString().Substring(4).Length - 5) + ".aspx";
Thursday, August 28, 2003 2:33 PM
To clarify a bit on this, the "ASP.test_aspx" that ToString() returns is a result of the default implementation of Object.ToString(), which is to return Object.GetType().FullName. Basically, "ASP.<file-name>_<file-extension>" is ASP.NET's internal format for generating strongly-typed objects based on the contents of a file. You should not depend on this behaviour, as it is an internal detail of ASP.NET's implementation, and is by no means guaranteed to remain the same in future versions. My advice would be to use Request.PhysicalPath to get the physical location of the requested file, then use the System.IO.Path.GetFileNameWithoutExtension() method to extract only the file's name. Hope this helps. -- Ryan Milligan
Thursday, December 13, 2007 3:52 AM
Try this:
Path.GetFileName(request.PhysicalPath);
Monday, March 22, 2010 9:22 AM
Thanks man. Yours worked and also worked fine too
Monday, March 22, 2010 11:27 AM
Hi,
try this:
string filename = Path.GetFileName(Request.Path);
regards
Robert
Wednesday, July 7, 2010 11:53 PM
I've also tried de Page.ToString() but If MS change this it won't work anymore.
I've finally use this to get only the filename + extension of the file
string[] file = Request.CurrentExecutionFilePath.Split('/');
string fileName = file[file.Length-1];
Pretty basic, I hope this help.
Martin.
Friday, August 27, 2010 3:02 PM
Hi,
try this:
string filename = Path.GetFileName(Request.Path);
Hi,
try this:
string filename = Path.GetFileName(Request.Path);
regards
Robert
Thanks a lot, it helped me!
Friday, October 8, 2010 5:30 AM
could be useful:
Path.GetFileName(Request.CurrentExecutionFilePath.ToLower())