繁体   English   中英

带有QueryString的AbsolutePath

[英]AbsolutePath with QueryString

我有以下代码:

     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;
        }

如何简化上面的代码,以便使用任何查询字符串。

使用UriBuilder并更换您需要的部件。 就像是:

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;
    }

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

暂无
暂无

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

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