C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Quote: Because the HttpContext.Request property is of type HttpRequest, the ASP.NET intrinsic Request object has all the properties and methods of HttpRequest automatically available to it.
Quote: Thus to get the length, in bytes, of the content sent by the client browser, you only need to specify the Request.ContentLength (Microsoft Docs).
Info: The difference is that HttpContext.Current.Request is accessed through a static property.
PropertyAnd: Request is accessed directly from the base class HttpApplication. Using HttpContext.Current.Request introduces overhead to your call.
Example code: C#
//
// 1.
//
string physical = Request.PhysicalPath;
//
// 2.
//
string physical2 = HttpContext.Current.Request.PhysicalPath;
Intermediate language instructions 1
IL_0023: call instance class [System.Web]System.Web.HttpRequest
[System.Web]System.Web.HttpApplication::get_Request()
IL_0028: callvirt instance string [System.Web]System.Web.HttpRequest::
get_PhysicalPath()
Intermediate language instructions 2
IL_002e: call class [System.Web]System.Web.HttpContext
[System.Web]System.Web.HttpContext::get_Current()
IL_0033: callvirt instance class [System.Web]System.Web.HttpRequest
[System.Web]System.Web.HttpContext::get_Request()
IL_0038: callvirt instance string [System.Web]System.Web.HttpRequest::
get_PhysicalPath()
So: It is best to just directly access Request, Server, Context or Response objects.
Tip: In our programs, we should directly use the HttpApplication and avoid calling HttpContext at all.
And: This will result in smaller IL and probably is faster. Simpler is better and usually much faster.
Before and after example code: C#
//
// A.
// Before optimization
//
Response.Write(HttpContext.Current.Server.HtmlEncode(terms));
//
// A.
// After optimization
//
Response.Write(Server.HtmlEncode(terms));
//
// B.
// Before optimization
//
HttpContext.Current.RewritePath("~/default.aspx?file=" + key, false);
//
// B.
// After optimization
//
Context.RewritePath("~/default.aspx?file=" + key, false);
Also: We used the IL Disassembler tool to gain insight into how ASP.NET internally works.