2013-11-04 63 views
0

我想从所有重定向请求中删除查询参数'mobile'。 Redirect.aspx页面将访问者重定向到Default.aspx?mobile = 1。当访问者浏览到重定向,aspx时,最终他应该被引导到Default.aspx,而不在地址栏中添加参数。 我采取的步骤: 因此,如果当前请求是重定向,我必须从查询字符串中删除查询参数“mobile”。这里是问题:我正在检查状态码是否为3xx,查询是否有'mobile'参数,但这种情况永远不会等于true。检查重定向是否改变查询字符串

Redirect.aspx:

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 
    Context.Response.Redirect("Default.aspx?mobile=1"); 
} 

RemoveParamModule:

public class RemoveParamModule : IHttpModule 
{ 
    public void Init(HttpApplication context) 
    { 
     context.EndRequest += RewriteHandler; 
    } 

    private void RewriteHandler(object sender, EventArgs eventArgs) 
    { 
     var context = (HttpApplication)sender; 
     var statusCode = context.Response.StatusCode; 
     if (statusCode.IsInRange(300, 399) && context.Request.QueryString["mobile"] != null) 
     { 
      DeleteMobileParameter(context.Request.QueryString); 
      context.Response.Redirect(context.Request.Path, true); 
     } 
    } 

    private static void DeleteMobileParameter(NameValueCollection collection) 
    { 
     var readOnlyProperty = collection.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); 
     readOnlyProperty.SetValue(collection, false, null); 
     collection.Remove("mobile"); 
     readOnlyProperty.SetValue(collection, true, null); 
    } 

    public void Dispose() 
    { 
    } 
} 

为什么模块中的请求或者具有的StatusCode = 302或参数 '移动',但从来没有同时在同一时间?我怎样才能删除重定向参数'移动'?

回答

1

Response.Redirect从服务器为以前请求的URL创建响应。然后,客户端浏览器收到此响应,并获取新的URL,该服务器将使用通常的200结果进行处理。

所以基本上:

Request: GET Response.aspx 
Response: 302 Default.aspx?mobile=1 

Request: GET Default.aspx?mobile=1 
Response: 200 <body> 

所以,如果我理解正确的您的需求 - 你不应该从请求 URL解析mobile,但分析你响应代替。

Response.Redirect可能会抛出ThreadAbortException所以要小心在同一管道中的几个重定向。

+0

感谢您的解释,但我从查询字符串解析'移动'。它现在开始工作,只是因为我的故事中的Response.aspx应该用'mobile'参数来调用。 – myroman