Kar*_*k N 8 c# asp.net ajax redirect
根据以下链接,当您在更新面板内的任何帖子中执行"Response.Redirect"时,它将发送一个200的HTTPResponseCode,并且Ajax javascripty库将从那里接管以将用户重定向到页面.
但是,我正在编写一个模块,我在其中拦截所有重定向并更改URL以包含值.我在PreSendRequestContent事件中这样做.以下是我的代码
HttpApplication app = (HttpApplication)sender;
if (app.Response.StatusCode == 302 && !app.Response.RedirectLocation.Contains("MyValue"))
{
// Add MyValue to URL
}
Run Code Online (Sandbox Code Playgroud)
在上述重定向过程中,有什么方法可以让我这样做吗?我可以在客户端使用onEndRequest或类似的事件以及服务器端的一些其他模块事件来做这个
这是我发现的一个肮脏的解决方案。我在客户端添加了以下代码。
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
function beginRequest(sender, args) {
args._request._events.addHandler('completed', changeURL);
}
function changeURL(sender, args) {
var s = new String(sender._xmlHttpRequest.responseText);
if (s.indexOf('pageRedirect') > -1)
{
var pattern = 'pageRedirect||';
var s1 = s.substring(s.indexOf(pattern) + pattern.length, s.length);
var nextPipe = s1.indexOf('|');
var encodedURL = s1.substring(0, nextPipe);
encodedURL = decodeURIComponent(encodedURL);
window.location.href = DoCustomizationsOntheURL(encodedURL);
args.stopPropagation();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我对此解决方案不满意并正在寻找更好的方法,请建议其他方法。