views:

471

answers:

3
+1  A: 
protected void Page_Load(object sender, EventArgs e)
{
    string cssFileName = Path.GetFileName(this.Request.PhysicalPath).Replace(".aspx", ".css");
}
JamesBrownIsDead
Why replace .aspx with .css?
John K
@jdk: I think JamesBrownIsDead copied and pasted from one of his existing code. But the general idea is there for all to see.
o.k.w
+6  A: 

System.IO.Path.GetFileName(Request.PhysicalPath);

o.k.w
A: 

Some short answers are already taken so, for fun, and because you'll likely want to do this from other Web Forms, here's an expanded solution that will affect all Web Forms in your project uniformly (includes code to get a filename as requested).

Make an extension method for the System.Web.UI.Page class by putting this code in a file. You need to use .NET 3.5.

namespace MyExtensions {
    using System.Web.UI;

    static public class Extensions {

        /* You can stuff anybody else's logic into this
         *  method to get the page filename, whichever implementation you prefer.
         */
        static public string GetFilename(this Page p) {
            // Extract filename.
            return p.AppRelativeVirtualPath.Substring(
                p.AppRelativeVirtualPath.IndexOf("/") + 1
                );
        }

    }
}

To get the filename from any ASP.NET Web Form (for example in the load method you specified):

    using MyExtensions;

    protected void Page_Load(object sender, EventArgs e) {
        string aspxFileName = this.GetFilename();
    }

Call this method on any Web Form in your project.

John K
Please don't add code to system namespaces. It's a thoroughly bad idea.
Jon Skeet
Yes, I figured some comments about the namespace usage might come out since I reference this post from my new namespace question over here http://stackoverflow.com/questions/1813746/should-i-put-custom-code-inside-microsofts-bcl-fcl-namespaces - so I'm going to edit this code sample to use custom namespaces.
John K
Okay, finished modifying this sample's namespace usage to conform to popular practice, and unlinked this code sample from aforementioned link.
John K