Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 初始化BasePage时HttpContext.Session为空_C#_Asp.net_Webforms - Fatal编程技术网

C# 初始化BasePage时HttpContext.Session为空

C# 初始化BasePage时HttpContext.Session为空,c#,asp.net,webforms,C#,Asp.net,Webforms,我有一个从中继承的基页 public class BasePage : System.Web.UI.Page { protected readonly ValueExtractor ValueExtractor; protected sealed override HttpContext Context => base.Context; public BasePage() { ValueExtractor = new ValueExtrac

我有一个从中继承的基页

public class BasePage : System.Web.UI.Page
{
    protected readonly ValueExtractor ValueExtractor;
    protected sealed override HttpContext Context => base.Context;

    public BasePage()
    {
        ValueExtractor = new ValueExtractor(new HttpContextWrapper(Context));
        ClientTimeZone = new TimeZoneHelper(OrganizationId);
    }

    public int OrganizationId
    {
        get { return ValueExtractor.ExtractValFromSession<int>("OrgId"); }
        set { Session["OrgId"] = value; }
    }
}
公共类基页:System.Web.UI.Page
{
受保护的只读ValueExtractor ValueExtractor;
受保护的密封覆盖HttpContext=>base.Context;
公共基页()
{
ValueExtractor=新的ValueExtractor(新的HttpContextWrapper(上下文));
ClientTimeZone=新时区帮助程序(OrganizationId);
}
公共int组织ID
{
获取{return ValueExtractor.ExtractValFromSession(“OrgId”);}
设置{Session[“OrgId”]=value;}
}
}
我遇到的问题是OrganizationId返回0。

ValueExtractor.ExtractValFromSession如下所示

public T ExtractValFromSession<T>(string key, bool throwException = true)
{
    var ret = default(T);
    var typeParameterType = typeof(T);
    if (_context.Session == null) return ret; // The problem occurs here because Session is null
    var val = _context.Session[key];
    if (val != null)
    {
        try
        {
            if (typeParameterType == typeof(int))
            {
                int r;
                int.TryParse(val.ToString(), out r);
                ret = (T)Convert.ChangeType(r, typeof(T), CultureInfo.InvariantCulture);
            }
            else if (typeParameterType == typeof(bool))
            {
                bool r;
                bool.TryParse(val.ToString(), out r);
                ret = (T)Convert.ChangeType(r, typeof(T), CultureInfo.InvariantCulture);
            }
            else if(typeParameterType == typeof(string))
                ret = (T)Convert.ChangeType(val.ToString(), typeof(T), CultureInfo.InvariantCulture);
            else
                ret = (T)Convert.ChangeType(val, typeof(T), CultureInfo.InvariantCulture);

        }
        catch (Exception ex)
        {
            throw new ApplicationException($"ExtractValFromSession error: {ex.Message}");
        }
    }
    else
    {
        return ret;
    }
    return ret;
}
public T ExtractValFromSession(字符串键,bool throweException=true)
{
var-ret=默认值(T);
var-typeParameterType=typeof(T);
if(_context.Session==null)return ret;//此处出现问题,因为会话为null
var val=_context.Session[key];
如果(val!=null)
{
尝试
{
if(typeParameterType==typeof(int))
{
INTR;
int.TryParse(val.ToString(),out r);
ret=(T)Convert.ChangeType(r,typeof(T),CultureInfo.InvariantCulture);
}
else if(typeParameterType==typeof(bool))
{
布尔r;
TryParse(val.ToString(),out r);
ret=(T)Convert.ChangeType(r,typeof(T),CultureInfo.InvariantCulture);
}
else if(typeParameterType==typeof(string))
ret=(T)Convert.ChangeType(val.ToString(),typeof(T),CultureInfo.InvariantCulture);
其他的
ret=(T)Convert.ChangeType(val,typeof(T),CultureInfo.InvariantCulture);
}
捕获(例外情况除外)
{
抛出新的ApplicationException($“ExtractValFromSession错误:{ex.Message}”);
}
}
其他的
{
返回ret;
}
返回ret;
}

您无法访问页面构造函数中的会话,您需要将此代码移动到
page\u load
或页面生命周期的其他方法


更多信息可以是

您不需要声明上下文。您正在从该页继承,因此您已经可以访问该页。