我正在尝试在C#中创建查询字符串。我在StackOverflow中找到了此代码,我真的很喜欢它,并希望在我的项目中使用。但是,我得到一个错误,并且坚持下去。这是代码
public static string AddQueryParam(this string source, string key, string value)
{
string delim;
if ((source == null) || !source.Contains("?"))
{
delim = "?";
}
else if (source.EndsWith("?") || source.EndsWith("&"))
{
delim = string.Empty;
}
else
{
delim = "&";
}
return source + delim + HttpUtility.UrlEncode(key)
+ "=" + HttpUtility.UrlEncode(value);
}
private string QueryStringCreator()
{
string queryString = "http://www.something.com/something.html"
.AddQueryParam("name", "jason")//I get the error here
.AddQueryParam("age","26");
return queryString;
}
错误是:
'string'不包含'AddQueryParam'的定义,找不到扩展方法'AddQueryParam'接受类型为'string'的第一个参数(是否缺少using指令或程序集引用?)
我怎么解决这个问题?谢谢。
要制作扩展方法AddQueryParam
,请将其放在单独的静态类中。
static class StringExtension
{
public static string AddQueryParam(this string source, string key, string value)
{
// ...
}
}
顺便说一句,我希望发布的代码会出现另一个错误:
扩展方法必须在非通用静态类中定义
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句