.NET CORE中通过ActionFilterAttribute访问Session

Luk*_*ler 5 .net-core asp.net-core-2.1

我尝试使用.NET CORE中的ActionFilterAttribute来访问会话值。因此我使用以下代码:

一个普通的 .NET Core 控制器,它获取注释 [SessionCheck]。

[SessionCheck]
public class HomeController : Controller
{
}
Run Code Online (Sandbox Code Playgroud)

这些类由 ActionFilterAttribute 类扩展,并覆盖 OnActionExecuted 方法,我想在其中访问会话值。有可能吗?我只能访问密钥,但不能访问会话值。

public class SessionCheck:ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var session = context.HttpContext.Session;
        //var mySessionValue = session["user"];
    }
}
Run Code Online (Sandbox Code Playgroud)

我的主要问题是,我有一个会话值,它描述了我必须将请求路由到的位置。如果有特殊值,我想将我的请求路由到位置 A,否则路由到位置 B。

还有另一种可能性吗?

Sur*_*shi 8

我有同样的问题,你可以执行如下操作:

filterContext.HttpContext.Session.GetString("user");
Run Code Online (Sandbox Code Playgroud)