簡體   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