Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
ASP.NET会话对象在尝试执行“会话”时生成InvalidCastException;“有效”;铸造_Asp.net_Session_Casting - Fatal编程技术网

ASP.NET会话对象在尝试执行“会话”时生成InvalidCastException;“有效”;铸造

ASP.NET会话对象在尝试执行“会话”时生成InvalidCastException;“有效”;铸造,asp.net,session,casting,Asp.net,Session,Casting,在方法中,我将结构存储到HttpSession中: HttpContext.Current.Session["search_headers"] = ra; 其中ra是类型为“replysearchax”的自定义结构 在属于同一命名空间的webmethod中,我尝试执行以下操作: [WebMethod(EnableSession = true)] public replySearchAjax RestorePhenSearchTable() { if (

在方法中,我将结构存储到HttpSession中:

  HttpContext.Current.Session["search_headers"] = ra;
其中ra是类型为“replysearchax”的自定义结构

在属于同一命名空间的webmethod中,我尝试执行以下操作:

   [WebMethod(EnableSession = true)]
    public replySearchAjax RestorePhenSearchTable()
    {
        if (HttpContext.Current.Session["search_headers"] != null)
        {
            replySearchAjax aa =  (replySearchAjax) HttpContext.Current.Session["search_headers"];
            return aa;
        }
但是带有typecast的行返回

{"Message":"Specified cast is not valid." 
 "ExceptionType":"System.InvalidCastException"}
在生成异常的行的断点处检查会话变量,可以发现其中有有效的ReplyseArchJAX字段


我将如何使它工作?提前谢谢

使用struct有什么原因吗?如果将ReplySearchJAX更改为类,是否会出现相同的错误?@JasonP,没有帮助。:)Simliar typecase错误。当您首先存储“ra”时,您是否绝对确定它是正确的类型?您能否在问题的第一行代码处设置断点并进行检查?