Is there any way to get the file name for the class?

Is there a way to get the class file name?

In particular, I would like to create a static method (CreateLink) in the base class (BasePage) to automatically return the path and file name of the called page.

I am code in C # ASP.NET

private const string TEMPLATE = "~/One.aspx"; public static HyperLink CreateLink() { HyperLink link = new HyperLink(); link.Text = "Click here"; link.NavigateUrl = String.Format(TEMPLATE); return link; } 

Is it possible to avoid using a hard-coded TEMPLATE variable? Is it possible to get the One.aspx path from the file name and location?

+4
source share
3 answers

You can get the path to the current page request by setting HttpContext.Current.Request.Path .

+6
source

what do you mean by class file name? Do you need to get the file name from the given path? try it

 Path.GetFileName(HttpContext.Current.Request.Path) 
+2
source

To get the actual file name using reflection, you can use the following:

 using System.Diagnostics; StackTrace stackTrace = new StackTrace(true); StackFrame frame = stackTrace.GetFrame(0); string s = frame.GetFileName(); 
+1
source

Source: https://habr.com/ru/post/1312334/


All Articles