HttpContextBase is a class in the Microsoft.AspNet.WebPages.WebPageBase namespace that provides a base class for objects that contain information about an HTTP request. This class is used extensively in ASP.NET applications to access and manipulate various properties of the current HTTP request and response.
Here are a few examples of how HttpContextBase can be used in C#:
1. Retrieving a query parameter from the current request:
HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); string queryParam = httpContext.Request.QueryString["param"];
This code snippet creates an instance of HttpContextWrapper using the HttpContext.Current property and retrieves the value of a query parameter named "param".
2. Adding a custom HTTP header to the response:
HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); httpContext.Response.AddHeader("X-MyHeader", "MyValue");
This code snippet creates an instance of HttpContextWrapper using the HttpContext.Current property and adds a custom header to the HTTP response.
3. Retrieving the current user's identity:
HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); string userName = httpContext.User.Identity.Name;
This code snippet creates an instance of HttpContextWrapper using the HttpContext.Current property and retrieves the current user's identity (assuming the application is using some form of authentication).
The HttpContextBase class is part of the System.Web.Abstractions package library, which is included in the Microsoft.AspNet.WebPages package. This package can be installed using the NuGet Package Manager in Visual Studio.
C# (CSharp) HttpContextBase - 60 examples found. These are the top rated real world C# (CSharp) examples of HttpContextBase extracted from open source projects. You can rate examples to help us improve the quality of examples.