Encapsulates all HTTP-specific information about an individual HTTP request.
public ref class HttpContext sealed : IServiceProvider
public sealed class HttpContext : IServiceProvider
type HttpContext = class
interface IServiceProvider
Public NotInheritable Class HttpContext
Implements IServiceProvider
The following example demonstrates how to access and display properties of the HttpContext object. The context of the current HTTP request is accessed by using the Context property of the Page object.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// The HttpContext associated with the page can be accessed by the Context property.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
// Use the current HttpContext object to determine if custom errors are enabled.
sb.Append("Is custom errors enabled: " +
Context.IsCustomErrorEnabled.ToString() + "<br/>");
// Use the current HttpContext object to determine if debugging is enabled.
sb.Append("Is debugging enabled: " +
Context.IsDebuggingEnabled.ToString() + "<br/>");
// Use the current HttpContext object to access the current TraceContext object.
sb.Append("Trace Enabled: " +
Context.Trace.IsEnabled.ToString() + "<br/>");
// Use the current HttpContext object to access the current HttpApplicationState object.
sb.Append("Number of items in Application state: " +
Context.Application.Count.ToString() + "<br/>");
// Use the current HttpContext object to access the current HttpSessionState object.
// Session state may not be configured.
try
{
sb.Append("Number of items in Session state: " +
Context.Session.Count.ToString() + "<br/>");
}
catch
{
sb.Append("Session state not enabled. <br/>");
}
// Use the current HttpContext object to access the current Cache object.
sb.Append("Number of items in the cache: " +
Context.Cache.Count.ToString() + "<br/>");
// Use the current HttpContext object to determine the timestamp for the current HTTP Request.
sb.Append("Timestamp for the HTTP request: " +
Context.Timestamp.ToString() + "<br/>");
// Assign StringBuilder object to output label.
OutputLabel.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpContext Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Using the current HttpContext to get information about the current page.
<br />
<asp:Label id="OutputLabel" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' The HttpContext associated with the page can be accessed by the Context property.
Dim sb As New System.Text.StringBuilder()
' Use the current HttpContext object to determine if custom errors are enabled.
sb.Append("Is custom errors enabled: " & _
Context.IsCustomErrorEnabled.ToString() & "<br/>")
' Use the current HttpContext object to determine if debugging is enabled.
sb.Append("Is debugging enabled: " & _
Context.IsDebuggingEnabled.ToString() & "<br/>")
' Use the current HttpContext object to access the current TraceContext object.
sb.Append("Trace Enabled: " & _
Context.Trace.IsEnabled.ToString() & "<br/>")
' Use the current HttpContext object to access the current HttpApplicationState object.
sb.Append("Number of items in Application state: " & _
Context.Application.Count.ToString() & "<br/>")
' Use the current HttpContext object to access the current HttpSessionState object.
' Session state may not be configured.
Try
sb.Append("Number of items in Session state: " & _
Context.Session.Count.ToString() & "<br/>")
Catch ex As Exception
sb.Append("Session state not enabled. <br/>")
End Try
' Use the current HttpContext object to access the current Cache object.
sb.Append("Number of items in the cache: " & _
Context.Cache.Count.ToString() & "<br/>")
' Use the current HttpContext object to determine the timestamp for the current HTTP Request.
sb.Append("Timestamp for the HTTP request: " & _
Context.Timestamp.ToString() & "<br/>")
' Assign StringBuilder object to output label.
OutputLabel.Text = sb.ToString()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpContext Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Using the current HttpContext to get information about the current page.
<br />
<asp:Label id="OutputLabel" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Classes that inherit the IHttpModule and IHttpHandler interfaces are provided a reference to an HttpContext object for the current HTTP request. The object provides access to the intrinsic Request, Response, and Server properties for the request.
Important
This object is ready for garbage collection when the HttpRequest is completed. Its usage after the request completes could lead to undefined behavior, such as a NullReferenceException.
This object is only available in the thread controlled by ASP.NET. Usage in background threads could lead to undefined behavior.
Constructors Properties Methods AcceptWebSocketRequest(Func<AspNetWebSocketContext,Task>, AspNetWebSocketOptions)Accepts an AspNetWebSocket request using the specified user function and options object.
AcceptWebSocketRequest(Func<AspNetWebSocketContext,Task>)Accepts an AspNetWebSocket request using the specified user function.
AddError(Exception)Adds an exception to the exception collection for the current HTTP request.
AddOnRequestCompleted(Action<HttpContext>)Raises a virtual event that occurs when the HTTP part of the request is ending.
ClearError()Clears all errors for the current HTTP request.
DisposeOnPipelineCompleted(IDisposable)Enables an object's Dispose() method to be called when the AspNetWebSocket connection part of this request is completed.
Equals(Object)Determines whether the specified object is equal to the current object.
(Inherited from Object) GetAppConfig(String)Obsolete.
Returns requested configuration information for the current application.
GetConfig(String)Obsolete.
Returns requested configuration information for the current HTTP request.
GetGlobalResourceObject(String, String, CultureInfo)Gets an application-level resource object based on the specified ClassKey and ResourceKey properties, and on the CultureInfo object.
GetGlobalResourceObject(String, String)Gets an application-level resource object based on the specified ClassKey and ResourceKey properties.
GetHashCode()Serves as the default hash function.
(Inherited from Object) GetLocalResourceObject(String, String, CultureInfo)Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties, and on the CultureInfo object.
GetLocalResourceObject(String, String)Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties.
GetSection(String)Gets a specified configuration section for the current application's default configuration.
GetType()Gets the Type of the current instance.
(Inherited from Object) MemberwiseClone()Creates a shallow copy of the current Object.
(Inherited from Object) RemapHandler(IHttpHandler)Enables you to specify a handler for the request.
RewritePath(String, Boolean)Rewrites the URL using the given path and a Boolean value that specifies whether the virtual path for server resources is modified.
RewritePath(String, String, String, Boolean)Rewrites the URL using the given virtual path, path information, query string information, and a Boolean value that specifies whether the client file path is set to the rewrite path.
RewritePath(String, String, String)Rewrites the URL by using the given path, path information, and query string information.
RewritePath(String)Rewrites the URL using the given path.
SetSessionStateBehavior(SessionStateBehavior)Sets the type of session state behavior that is required in order to support an HTTP request.
ToString()Returns a string that represents the current object.
(Inherited from Object) Explicit Interface Implementations See alsoCollaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4