I have an ASPX page named Default.aspx
. From its codebehind on Page_Load()
, I would like to get "Default.aspx", alone, into a string:
protected void Page_Load(object sender, EventArgs e)
{
string aspxFileName = ?;
}
What should I replace ?
with—what will get me the ASPX filename?
string path = Server. MapPath(Page. AppRelativeVirtualPath);
You can just make a System. Uri object, and use IsFile to verify it's a file, then Uri. LocalPath to extract the filename. This is much safer, as it provides you a means to check the validity of the URI as well.
CSHTML files run on a web server, which generates HTML for a client web browser. CSHTML files are similar to . VBHTML (Visual Basic HTML) files. However, they use syntax that is closer to the C# language than the Visual Basic language.
System.IO.Path.GetFileName(Request.PhysicalPath);
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