我有一个扩展 HttpResponse 功能的 HttpResponseExtensions 类。我想在 AJAX 请求中添加重定向用户的方法。所以这是我的检查方法:
public static void RedirectUser(this HttpResponse response, string url)
{
if (response.IsRequestBeingRedirecting())
return;
var context = HttpContext.Current;
if (context != null)
{
context.ApplicationInstance.CompleteRequest();
}
if (context != null && context.Request.IsAjaxRequest())
{
context.Items["IsRedirecting"] = true;
RegisterRedirectScript(response,url);
}
else
{
response.Redirect(url, false);
}
}
这是 RegisterRedirectScript 方法的实现
public static void RegisterRedirectScript(this HttpResponse response, string url)
{
var context = HttpContext.Current;
if (context != null)
{
context.Response.Write(string.Format("<script type='text/javascript'>window.location.href = '{0}';</script>", url));
}
}
当会话无效时,我正在调用 RedirectUser。但是,如果请求是 ajax 之一:我收到以下错误:
The Message recived from the server could not be parsed. Common causes for this error are the response is modified calls to response.write(), response details , HttpModule, OR server trace is enabled. Error detailes next to : window.locat.
我知道我可以使用 radAjaxManager、ClientScriptManager 或 ScriptManager 注册脚本。但是我怎样才能为 HttpResponse 类做呢?
任何帮助表示赞赏。