简体   繁体   中英

I use “HttpContext.Current” but debug got error “HttpContext' does not contain a definition for 'Current”

[.Net Core 3.1]

I need to set SessionTimeout redirect to login page. How can I use HttpContext.Current? This is my code

using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Http;

namespace DemoMVCLogin.Class
{
  public class SessionManagement
  {

  }


  public class SessionTimeoutAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
        HttpContext httpContext = HttpContext.Current;
        base.OnActionExecuting(filterContext);
      }
  }

}

HttpContext.Current is no longer part of.Net Core.

Access the HttpContext via ActionContext.HttpContext property

Gets or sets the HttpContext for the current request.

public class SessionTimeoutAttribute : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        HttpContext httpContext = filterContext.HttpContext;
        base.OnActionExecuting(filterContext);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM