How to get url of current page from C # App_Code class?

I have a logging class that, well, records things. I would like to add the ability to automatically record the current message page.

Is there any way to get the information I'm looking for?

Thanks,

+4
source share
4 answers

In your class, you can use the HttpContext.Current property (in System.Web.dll). From there you can create a property chain:

The main object is the page object, so if you apply it to it, use any object that you usually use from the Page object, for example, the Request property.

+5
source

It's fragile and hard to test, but you can use System.Web.HttpContext.Current , which will give you a Request , which in turn has the RawUrl property.

+2
source
public static class MyClass { public static string GetURL() { HttpRequest request = HttpContext.Current.Request; string url = request.Url.ToString(); return url; } } 

I tried to break it a bit :)

0
source

In the past, I also downloaded my own logging classes and used Console.Writeln (), but in fact there are many good logging options that already exist, so why go there? I use NLog almost everywhere; it is extremely flexible with various log output, including console and file, many log format options and trivially customize versions designed for various .net infrastructures, including compact ones. Starting the installer will add the NLog configuration file settings to the Visual Studio Add New Item dialog. Using in your code is simple:

 // declare in your class private static Logger logger = LogManager.GetCurrentClassLogger(); ... // use in your code logger.Debug(() => string.Format("Url: {0}", HttpContext.Current.Request.Url)); 
0
source

All Articles