2013-10-04 66 views
1

我有以下代码:AbsolutePath与查询字符串

 if (Request.Url.AbsolutePath == "/Guidance.aspx") 
     { 
      if (Request.IsSecureConnection) 
      { 
       Reponse.Redirect("http://www.example.com/Guidance.aspx"); 
      } 
      return; 
     } 

的事情是,指导可以用它查询字符串。我喜欢重定向到相同的页面名称并追加查询字符串。还没有找到办法做到这一点。

 if (Request.Url.AbsolutePath == "/Guidance.aspx?id='vid09'") 
     { 
      if (Request.IsSecureConnection) 
      { 
       Reponse.Redirect("http://www.example.com/Guidance.aspx?id='vid09'"); 
      } 
      return; 
     } 

我怎样才能简化上面的代码来处理任何查询字符串。

+0

查看字符串是否包含'Guidance.aspx'preg_match('/(Guidance.aspx)/')',然后将所有'?'后面的内容放入变量中,并在其重定向时将其追加到末尾 – Luke

+0

使用原始网址提取查询字符串,只是将相同的查询字符串附加到RedirectUrl,没有简单的方法处理C#中的查询字符串,我记得.. –

+0

也许你可以使用IIS URL重写? http://www.iis.net/downloads/microsoft/url-rewrite –

回答

0
string myUrl = Request.RawUrl.toString(); 
if (myUrl.Contains("/Guidance.aspx") 
    { 
     if (Request.IsSecureConnection) 
     { 
      var queryString = myUrl.Substring(myUrl.IndexOf("?")); 
      Reponse.Redirect("http://www.example.com/Guidance.aspx" + queryString); 
     } 
     return; 
    } 
+0

我不认为AbsoluatePath会给你查询字符串(Request.Url.AbsolutePath) –

+0

我的不好,编辑答案 –

+0

@NatePet其实它确实,我只是在LINQPad上测试它 –

3

使用UriBuilder并替换您需要的部件。喜欢的东西:

var builder = new UriBuilder(Request.Url); 
builder.Scheme = "http"; 
Reponse.Redirect(builder.ToString); 
0

不要花哨,URI是已经被解析为您(不自己不可靠的正则表达式做到这一点)。您使用的Url属性是一个System.Uri对象。您可以简单地比较方案,主机和您可能需要的任何HTTP段,然后通过仅添加来自原始URI的查询字符串组件来构建重定向URI。所有你需要的是在Uri类。