查询字符串 - 添加值在C#查询字符串 [英] Querystring - Add values to querystring in c#
问题描述
我如何添加值查询字符串?
How can I add values to querystring?
我试图做到这一点:
String currurl = HttpContext.Current.Request.RawUrl;
var querystring = HttpContext.Current.Request.QueryString.ToString();
var PrintURL = currurl + (String.IsNullOrEmpty(querystring)) ?
HttpContext.Current.Request.QueryString.Add("print", "y") : string.Empty;
不过,我不断收到此错误:
But I keep getting this error:
无法隐式转换类型'串'到'布尔
Cannot implicitly convert type 'string' to 'bool'
所有我想要做的就是当前网址,并添加?普林= y以查询字符串
all i'm trying to do is get current url and add ?pring=y to querystring
推荐答案
那么,第一个问题可以通过使用来解决这个代替:
Well, the first problem can be solved using this instead:
var PrintURL = currurl + (String.IsNullOrEmpty(querystring) ?
HttpContext.Current.Request.QueryString.Add("print", "y") : string.Empty);
所有这一切从原来的code改变时只需将关闭括号从(String.IsNullOrEmpty(查询字符串))
(它是不必要的),以结束的:
条款。这使得它明确清楚你想要做的事。
否则,编译器试图串联的 String.IsNullOrEmpty(查询字符串)
(这是一个布尔
)以<结果code> currUrl - 不正确的,你首先希望不要什么。
All that's changed from your original code is simply moving the closing paren from (String.IsNullOrEmpty(querystring))
(where it was unnecessary) to the end of the ?:
clause. This makes it explicitly clear what you're trying to do.
Otherwise, the compiler tries to concatenate the result of String.IsNullOrEmpty(querystring)
(which is a bool
) to currUrl
-- incorrect, and not what you intended in the first place.
不过,你有一个第二个问题 HttpContext.Current.Request.QueryString.Add(打印,Y)
语句。这将返回无效
,不是字符串
。你需要修改这部分的三元EX pression,使其返回一个字符串 - 什么是你想怎么办
However, you've got a second problem with the HttpContext.Current.Request.QueryString.Add("print", "y")
statement. This returns void
, not a string
. You'll need to modify this part of your ternary expression so that it returns a string -- what are you trying to do?
这篇关于查询字符串 - 添加值在C#查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!