I am trying to create a simple code to retrieve a string for the current url as follows:
string currentURL = HttpContext.Current.Request.Url.ToString(); However, I get the error upon running the code: Object reference not set to an instance of an object.
I assume I have to create an instance of HttpContext. The arguments for HttpContext are either HttpContext(HttpRequest request, HttpResponse response) or HttpContext(HttpWorkerRequest wr).
Is there documentation that details how to work with these arguments? I'm fairly new to C#, so I'm not entirely sure how to instantiate this object properly, and have not found any resources that have been helpful (including MS library).
HttpContext? Where is this code located? You don't want to create it on your own, it's created when routing occurs.ObjectReferenceexception doesn't mean that theCurrentcontext isnull. You have two other chained properties that could throw that error. Are you certain it's theCurrentcontext property?Currentcontext property, I was assuming that was where the exception was occurring. After inputting Mark's solution, I continue to get theObjectReferenceexception, so I am not sure where the issue lies within theHttpContextline.null. That will be step 1.