【问题标题】:Response is not available in this context asp.net响应在此上下文中不可用 asp.net
【发布时间】:2017-01-02 08:53:15
【问题描述】:

我正在开发一个 asp.net 网络应用程序。 Response.Redirect 抛出异常“响应在此上下文 asp.net 中不可用”。

有两种情况:

CASE1:当我直接从 default.aspx 页面使用 response.redirect 时

If(certain Condition) //This runs perfectly fine
{
     Session["UserId"] = intUserId;
     Session["SessionId"] = intSessionId;
     Session["Password"] = strPassword;
     Session["IsLoggedIn"] = Global.objOmniFlow.IsLoggedIn;
     Session["LastLogin"] = strLastLoginTime;
     Response.Redirect(CPCHostURL + QueryString);
}

CASE2:控件从 default.aspx.cs 转到一个 javascript 函数,从那里到 asp 处理程序页面,然后在 response.redirect 调用引发异常后再次返回 default.aspx.cs:

     Session["UserId"] = intUserId;
     Session["SessionId"] = intSessionId;
     Session["Password"] = strPassword;
     Session["IsLoggedIn"] = Global.objOmniFlow.IsLoggedIn;
     Session["LastLogin"] = strLastLoginTime;
     Response.Redirect(CPCHostURL + QueryString);//Exception here Response is not available in this context asp.net

javascript函数在default.aspx.cs文件中调用,函数为

 function abc()
      {

          var userName = '<%=Session["UserName"]%>';
          var cabinetName = '<%=Session["CabinetName"]%>';
          var moduleName = '<%=Session["ModuleName"]%>';
          var clearingType = '<%=Session["clearingType"]%>'; 
          var caseFlagtwo = '2';
          var oldPassword=  $("#pwd1").val()
          $.ajax({
              type: "POST",
              url: "LoginHandler1.ashx?userName=" + userName + "&cabinetName=" + cabinetName + "&moduleName=" + moduleName + "&oldPassword=" + encodeURIComponent(oldPassword) + "&caseFlag=" + caseFlagtwo + "&clearingType=" + clearingType,
              //data: {serviceIndex:serviceIndex, serviceStatus: serviceStatus, action: action },
              // dataType: "json",  
              success: function (outputString) {
                  alert(outputString);

              },
              error: function (outputString) {
                  // var result = json.name;
              }
          });
      }

接收这个的 ashx 文件是:

 public void ProcessRequest(HttpContext context)
    {
        String userName = context.Request["userName"].ToString();
        String cabinetName = context.Request["cabinetName"].ToString();
        String moduleName = context.Request["moduleName"].ToString();
        String caseFlag = context.Request["caseFlag"].ToString();
        String clearingType = context.Request["clearingType"].ToString();


            context.Response.ContentType = "text/plain";
            Default obj = new Default();
            obj.ForceLogin(userName, cabinetName, moduleName, clearingType);
        }

强制登录是我使用 response.redirect 的功能。

注意:当我使用的是 Response.Redirect 而不是

HttpContext.Current.Response.Redirect(CPCHostURL + QueryString);

那么没有例外,但我没有重定向到指定的 url,而是控件保留在我发送响应重定向的同一页面上

注意:CPCHostURL = http://192.168.56.210/CPCHost/

完整的 URL = http://192.168.56.210/CPCHost/?UserName=admin&Password=admin123&UserIndex=1&SessionId=934425343&Cabinet=maincab&LastLoginAt=2017-01-02 15:03:34&ClearingType=ok

【问题讨论】:

  • 问题出在您的完整网址中“?”之前不应该有“/”
  • @ManishGoswami 该 URL 是正确的,因为我已经告诉过,在情况 1 中,使用相同的 URL 可以正常工作。所以我认为问题是别的东西
  • 就目前而言,这个问题无法真正回答。您需要显示javascript如何 控件转到 javascript 函数 ashx 处理程序中的代码以及控件如何返回 default.aspx。
  • @user1429080 我已经更新了详细信息。看看你能不能找出我做错了什么。如果有任何疑问,请告知

标签: c# asp.net session-variables


【解决方案1】:

由于缺少数据,您的问题并不完全清楚,但据我所知,响应重定向应该可以工作。

我怀疑是您生成的 url,它似乎格式错误。

如果 CPCHostURL = http://192.168.56.210/CPCHost/,那么您之后直接连接查询字符串,这通常可能导致 http://192.168.56.210/CPCHost/?id=232&etc=232 这不是有效的 url。

发布您生成的网址以获得更好的答案。

【讨论】:

  • 我怀疑您的网址似乎有误,如果您手动将该网址粘贴到浏览器,您会得到想要的结果吗?
  • URL 是正确的,因为我已经告诉过,在情况 1 中,它在相同的 URL 下工作得非常好。所以我认为问题是别的东西
猜你喜欢
  • 2016-09-08
  • 2015-04-27
  • 1970-01-01
  • 2013-12-29
  • 2012-02-06
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多