【发布时间】: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