如何在不访问每个控制器中的 HttpContext 的情况下使会话变量对所有控制器可用?

How can I make a session variable available to all controllers without accessing HttpContext in each controller?

我正在尝试通过使用 HttpContext.Session.GetInt32("BranchId").Value.

int 变量 BranchId 在整个应用程序中可用

根据我自己的尝试以及对 this question 的回答,我了解到这种方法行不通:

基地控制器class:

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

namespace Vaktliste.Controllers
{
    public class MyBaseControllerClass : Controller
    {
        public readonly int branchId;

        public MyBaseControllerClass()
        {
            var s = HttpContext.Session; // <-- This causes a NULL exception
            // SET THE VARIABLE HERE:
            BranchId = s.GetInt32("BranchId") != null
                ? s.GetInt32("BranchId").Value
                : 0;
        }
    }
}

应用程序中另一个控制器的示例:

namespace Vaktliste.Controllers
{
    public class BranchesController : MyBaseControllerClass
    {
        private readonly VaktlisteContext db;

        public BranchesController(VaktlisteContext context)
        {
            db = context;
        }

        public async Task<IActionResult> Details(int? id)
        {
            // USE THE VARIABLE HERE:
            if (id == null) id = BranchId;

            if (id == null) return NotFound();

            var branch = await db.Branches
                .Where(m => m.Id == id)
                .FirstOrDefaultAsync();

            if (branch == null) return NotFound();

            HttpContext.Session.SetInt32("BranchId", branch.Id);

            return View(branch);
        }
    }
}

我当然可以在每个控制器中执行 GetInt32("BranchId"),但我想保持干燥(不要重复自己)。怎么做到的?

也许您可以使用 属性 来实现。创建基本控制器时,httpcontext.session 为空。如果您使用 属性 并且仅在您的路由中访问它,则该会话应该可用。

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

namespace Vaktliste.Controllers
{
    public class MyBaseControllerClass : Controller
    {
        public readonly int branchId;
        protected int BranchId { 
            get { HttpContext.Session?.GetInt32("BranchId")?.Value ?? 0; }
            set { HttpContext.Session.SetInt32("BranchId", value); }
        }

        public MyBaseControllerClass()
        {

        }
    }
}

然后访问属性得到值

namespace Vaktliste.Controllers
{
    public class BranchesController : MyBaseControllerClass
    {
        private readonly VaktlisteContext db;

        public BranchesController(VaktlisteContext context)
        {
            db = context;
        }

        public async Task<IActionResult> Details(int? id)
        {
            // USE THE VARIABLE HERE:
            id = id ?? BranchId;

            if (id == null) return NotFound();

            var branch = await db.Branches
                .Where(m => m.Id == id)
                .FirstOrDefaultAsync();

            if (branch == null) return NotFound();

            BranchId = branch.Id;

            return View(branch);
        }
    }
}

我会创建一个 protected 变量,然后用它来包装你的访问器:

protected int? BranchId
{
   get { return (HttpContext.Session["BranchId] as int?); }
   set { HttpContext.Session.SetInt32("BranchId", value);
}

然后将倒数第二行更新为:

this.BranchId = branch.Id;

当然,所有子类控制器都只访问受保护的变量。