简体   繁体   English

ASP.Net和NameValueCollection

[英]ASP.Net and NameValueCollection

I have the following method: 我有以下方法:

public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl)

The value of the fullUrl is: fullUrl的值为:

http://localhost:50435/page/view.aspx?si=00&us=admin&ut=smt& HTTP://本地主机:50435 /页/ view.aspx SI = 00&US =管理与UT = SMT&

When I do: 当我做:

NameValueCollection qscoll = HttpUtility.ParseQueryString(fullUrl);

I get: 我得到:

{http%3a%2f%2flocalhost%3a50435%2fpage%2fview.aspx%3fsi=00&us=admin&ut=smt&} {HTTP%3A%2F%2flocalhost%3a50435%2fpage%2fview.aspx%3fsi = 00&我们=管理员&UT = SMT&}

But I need to obtain the parameters in the QueryString of this page, and with this value, I cannot obtain the "si" value, because the question mark, that starts the querystring is encoded. 但是我需要获取此页面的QueryString中的参数,并且使用此值,我无法获取“ si”值,因为开始查询字符串的问号已编码。 So I thought: "humm... I should try to do the HttpUtility.HtmlEncode()" 所以我想:“哼...我应该尝试做HttpUtility.HtmlEncode()”

However the method HtmlEncode returns void: However the second parameter of this method sends the value to a TextWriter. 但是,方法HtmlEncode返回void:但是,此方法的第二个参数将值发送到TextWriter。 But it isn't the NameValueCollection. 但这不是NameValueCollection。

Maybe the solution is simple... but I cannot see it. 也许解决方案很简单...但是我看不到它。

You need to trim it down to just the querystring before parsing, like this: 您需要在解析之前将其缩减为仅查询字符串,如下所示:

if (fullUrl.Contains("?")) {
  fullUrl = fullUrl.Substring(fullUrl.IndexOf("?") + 1);
}
NameValueCollection qscoll = HttpUtility.ParseQueryString(fullUrl);

You could try: 您可以尝试:

var si = Request["si"];
var user = Request["us"];
//etc.

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

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