代码之家  ›  专栏  ›  技术社区  ›  John Gietzen

.NET URI:如何更改URI的一部分?

  •  24
  • John Gietzen  · 技术社区  · 16 年前

    我经常想改变只是 一个URI的一部分,并返回一个新的URI对象。

    在我目前的困境中,我想附加 .nyud.net ,使用coralcdn。

    我有一个完全合格的URI fullUri . 实际上,我如何才能做到这一点:

    fullUri.Host = fullUri.Host + ".nyud.net";
    

    这需要适用于几乎所有的URL,并且需要维护请求的端口。

    任何帮助都将不胜感激。

    1 回复  |  直到 16 年前
        1
  •  48
  •   dtb    16 年前

    你可以使用 UriBuilder 修改 Uri :

    Uri uri = new Uri("http://stackoverflow.com/questions/2163191/");
    
    UriBuilder builder = new UriBuilder(uri);
    builder.Host += ".nyud.net";
    
    Uri result = builder.Uri;
    // result is "http://stackoverflow.com.nyud.net/questions/2163191/"
    
    推荐文章