c# - How to properly instantiate HttpContext object? -
i trying create simple code retrieve string current url follows:
string currenturl = httpcontext.current.request.url.tostring(); however, error upon running code: object reference not set instance of object.
i assume have create instance of httpcontext. arguments httpcontext either httpcontext(httprequest request, httpresponse response) or httpcontext(httpworkerrequest wr).
is there documentation details how work these arguments? i'm new c#, i'm not entirely sure how instantiate object properly, , have not found resources have been helpful (including ms library).
the httpcontext object instantiated, once per request thread, asp.net infrastructure. you have running asp.net on web server (e.g., iis) available. not meant initialized in user code. accessing instance through httpcontext.current static property. it null if not running asp.net.
if wanted to, though, instantiate 1 based on request , response of existing httpcontext:
var request = httpcontext.current.request; var response = httpcontext.current.response; var newcontext = new httpcontext(request, response);
Comments
Post a Comment