简体   繁体   English

带有QueryString的AbsolutePath

[英]AbsolutePath with QueryString

I have the following code: 我有以下代码:

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

The thing is that Guidance can have a querystring with it. 问题是指南可以有一个查询字符串。 I like to then Redirect to the same page name and append the querystring. 我喜欢然后重定向到相同的页面名称并附加查询字符串。 Haven't found a way to do this. 还没有找到办法做到这一点。

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

How can I simplify the code above to do it with any querystring that comes its way. 如何简化上面的代码,以便使用任何查询字符串。

Use UriBuilder and replace parts you need. 使用UriBuilder并更换您需要的部件。 Something like: 就像是:

var builder = new UriBuilder(Request.Url);
builder.Scheme = "http";
Reponse.Redirect(builder.ToString);
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;
    }

Don't get fancy, the URI is already parsed for you (don't do it yourself with unreliable regular expressions). 不要花哨,已经为你解析了URI(不要用不可靠的正则表达式自己做)。 The Url property you're using is a System.Uri object. 您正在使用的Url属性是System.Uri对象。 You may simply compare the scheme, host, and any HTTP segment you may need, then construct your redirection URI by adding only the query string component from the original URI. 您可以简单地比较方案,主机和您可能需要的任何HTTP段,然后通过仅添加原始URI中的查询字符串组件来构建重定向URI。 All you need is in the Uri class. 你所需要的就是Uri课程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM