Saturday, June 19, 2021

ASP.NET MVC Framework - HttpContext class Uses

  • The HttpContext associated with the view page can be accessed by the HttpContext property
  • Use the current HttpContext object to determine if custom errors are enabled.
  • Use the current HttpContext object to determine if debugging is enabled.
  • Use the current HttpContext object to access the current TraceContext object.
  • Use the current HttpContext object to access the current HttpApplicationState object.
  • Use the current HttpContext object to access the current HttpSessionState object.
  • Use the current HttpContext object to access the current Cache object.
  • Use the current HttpContext object to determine the timestamp for the current HTTP Request.


using System.Web.Mvc;

namespace WebAppHttp.Controllers
{
    public class ContextController : Controller
    {
        // GET: HttpContext
        public string Index()
        {
            // The HttpContext associated with the view page can be accessed by the HttpContext 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: " +
                HttpContext.IsCustomErrorEnabled.ToString() + "
"); // Use the current HttpContext object to determine if debugging is enabled. sb.Append("Is debugging enabled: " + HttpContext.IsDebuggingEnabled.ToString() + "
"); // Use the current HttpContext object to access the current TraceContext object. sb.Append("Trace Enabled: " + HttpContext.Trace.IsEnabled.ToString() + "
"); // Use the current HttpContext object to access the current HttpApplicationState object. sb.Append("Number of items in Application state: " + HttpContext.Application.Count.ToString() + "
"); // 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: " + HttpContext.Session.Count.ToString() + "
"); } catch { sb.Append("Session state not enabled.
"); } // Use the current HttpContext object to access the current Cache object. sb.Append("Number of items in the cache: " + HttpContext.Cache.Count.ToString() + "
"); // Use the current HttpContext object to determine the timestamp for the current HTTP Request. sb.Append("Timestamp for the HTTP request: " + HttpContext.Timestamp.ToString() + "
"); return sb.ToString(); } } }



OUTPUT
Is custom errors enabled: False
Is debugging enabled: True
Trace Enabled: False
Number of items in Application state: 0
Number of items in Session state: 0
Number of items in the cache: 7
Timestamp for the HTTP request: 2021-06-19 11:37:40 PM



No comments:

Post a Comment

Hot Topics