【问题标题】:Session returning null even though i can see the values即使我可以看到值,会话也返回 null
【发布时间】:2012-10-07 10:09:15
【问题描述】:
public class BaseController : Controller
{
    public string UserId { set; get; } 
    public string AccessToken { set; get; }

    private readonly IUnitOfWork _unit;

    public BaseController(IUnitOfWork unit)
    {
        _unit = unit;
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    { 
        UserId = Session["_UserId"] as string; 
        AccessToken = Session["_AccessToken"] as string;
        // ......
    }
 }

即使 AccessToken 返回值。即使我可以调试并看到 Session 对象中有一个值,UserId 仍以 null 出现。

我在会话中设置它们:

        Session.Add("_UserId", user.Id);
        Session.Add("_AccessToken", user.AccessToken);

发生了什么事?

【问题讨论】:

    标签: c# asp.net-mvc session


    【解决方案1】:
    UserId = Session["_UserId"] as string; 
    

    看起来键“_UserId”的值不是字符串 - 因此您得到null 作为结果。

    【讨论】:

    • :) 是的,没错。真可惜,即使它是 id 的 int,为什么它不转换为字符串。我有点期待。
    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多