ASP.NET:URI处理 [英] ASP.NET: URI handling

查看:174
本文介绍了ASP.NET:URI处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写信给 1 你好应该返回<$ C $,一个方法,该方法,让我们说C> http://something.com/?something=1&hello=en 。

我的可能的轻松破解了一起pretty,但什么抽象的功能并ASP.NET 3.5提供用于构建URI的?我想是这样的:

I could hack this together pretty easily, but what abstraction functionality does ASP.NET 3.5 provide for building URIs? I'd like something like:

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

我发现这听起来高度相关的乌里类,但我无法找到任何它可以完成上述真的。任何想法?

I found the Uri class which sounds highly relevant, but I can't find anything which does the above really. Any ideas?

(对于它的价值,参数的顺序并不重要,我。)

(For what it's worth, the order of the parameters doesn't matter to me.)

推荐答案

编辑,以纠正不正确大规模code

href=\"http://stackoverflow.com/questions/829080/how-to-build-a-query-string-for-a-url-in-c/829185#829185\">this回答以一个类似的问题,你可以轻松地做这样的事情:

Based on this answer to a similar question you could easily do something like:

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

这也将urlen code集合,所以:

This will also urlencode the collection, so:

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

将输出:

Something=1&hello=en%26that 

这篇关于ASP.NET:URI处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆