从类扩展中访问会话变量

时间:2016-09-09 10:59:31

标签: asp.net-core asp.net-core-mvc

是否可以从类扩展中访问会话变量?

我在会话中设置了这个变量:

var offset = 3;
HttpContext.Session.SetInt32("clienttimeoffset", offset);

在DateTime扩展中,我需要访问它:

public static DateTime? ToClientTime(this DateTime? value)
 {
    if (value == null)
         return null;

    var offset = get it here .....
    ....
}

1 个答案:

答案 0 :(得分:0)

不要在扩展程序中访问它。将它作为参数传递给扩展方法:

public static DateTime? ToClientTime(this DateTime? value, int offset)
 {
    if (value == null)
         return null;

        ....
}
相关问题