ASP.NET:URI处理

我正在写一个方法,比方说,1,你好,应该返回http://something.com/?something=1\u0026amp;hello=en.

我可以很容易地将它们组合在一起,但ASP.NET 3.5为构建URI提供了哪些抽象功能?我喜欢这样的东西:

URI uri = new URI("~/Hello.aspx"); // E.g. ResolveUrl is used here
uri.QueryString.Set("something","1");
uri.QueryString.Set("hello","en");
return uri.ToString(); // /Hello.aspx?something=1&hello=en

我发现Uri类听起来非常相关,但我找不到任何真正完成上述操作的内容.有任何想法吗?

(对于它的价值,参数的顺序对我来说无关紧要.)

解决方法

编辑纠正大量错误代码

基于this answer到类似的问题,您可以轻松地执行以下操作:

UriBuilder ub = new UriBuilder();

// You might want to take more care here,and set the host,scheme and port too
ub.Path = ResolveUrl("~/hello.aspx"); // Assumes we're on a page or control.

// Using var gets around internal nature of HttpValueCollection
var coll = HttpUtility.ParseQueryString(string.Empty);

coll["something"] = "1";
coll["hello"] = "en";

ub.Query = coll.ToString();
return ub.ToString();
// This returned the following on the VS development server:
// http://localhost/Hello.aspx?something=1&hello=en

这也将对集合进行urlencode,因此:

coll["Something"] = "1";
coll["hello"] = "en&that";

输出

Something=1&hello=en%26that

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....