'string'不包含C#的定义/错误

杰森

我正在尝试在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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Linq错误:“ string []不包含'Except'的定义。”

来自分类Dev

ValueTuple<(string LoanID, decimal x, ...)> 不包含 c# 中“loanID”的定义

来自分类Dev

c#对象不包含定义错误

来自分类Dev

C#-string []不包含“ SkipLast”的定义,并且没有可访问的扩展方法

来自分类Dev

.net核心3.1:'IAsyncEnumerable <string>'不包含'GetAwaiter'的定义

来自分类Dev

ISetup <IDurableOrchestrationContext,任务<List <string >>不包含'ReturnsAsync'的定义

来自分类Dev

System.Collections.Generic.List <string>'不包含'add'的定义

来自分类Dev

C#对象不包含方法的定义

来自分类Dev

C#“对象”不包含“拆分”的定义

来自分类Dev

C#对象不包含方法的定义

来自分类Dev

错误'System.Diagnostics.Process'在Visual Studio 2008 C#中不包含'GetProcesses'的定义

来自分类Dev

使用 C# 从 URL 获取内容后跟此错误“WebRequest 不包含 GetRespone 的定义和...”

来自分类Dev

使用 C# 从 URL 获取内容得到这个错误“WebRequest 不包含 GetRespone 的定义和……”

来自分类Dev

C ++成功编译,不包含<string>标头

来自分类Dev

C ++成功编译,不包含<string>标头

来自分类Dev

C#Roles.GetAllRoles()错误-不包含定义

来自分类Dev

System.Array不包含“任何”的定义-C#

来自分类Dev

CS1929 C#'RfcParameterClass'不包含'Cast'的定义

来自分类Dev

Foreach语句不包含C#上GetEnumerator的公共定义

来自分类Dev

dropdownlist控件asp.net C#错误为“ ASP.pract2_aspx”不包含“ dropDownList_Changed”的定义

来自分类Dev

C#错误CS1061:类型“ System.Collections.Generic.List <int>”不包含“长度”的定义

来自分类Dev

CS1061:类型“string”不包含“classChoice”的定义,也没有扩展方法“classChoice”

来自分类Dev

List(of String())包含String()

来自分类Dev

编辑不包含String的treeview的项目

来自分类Dev

Linq错误:不包含“ InsertOnSubmit”的定义

来自分类Dev

unity / vrtk不包含定义错误

来自分类Dev

Visual Studio 2012错误“不包含定义”

来自分类Dev

视图不包含 RemoveView 错误的定义

来自分类Dev

C#自定义Dictionary <string,string>接受重复的键进行序列化

Related 相关文章

  1. 1

    Linq错误:“ string []不包含'Except'的定义。”

  2. 2

    ValueTuple<(string LoanID, decimal x, ...)> 不包含 c# 中“loanID”的定义

  3. 3

    c#对象不包含定义错误

  4. 4

    C#-string []不包含“ SkipLast”的定义,并且没有可访问的扩展方法

  5. 5

    .net核心3.1:'IAsyncEnumerable <string>'不包含'GetAwaiter'的定义

  6. 6

    ISetup <IDurableOrchestrationContext,任务<List <string >>不包含'ReturnsAsync'的定义

  7. 7

    System.Collections.Generic.List <string>'不包含'add'的定义

  8. 8

    C#对象不包含方法的定义

  9. 9

    C#“对象”不包含“拆分”的定义

  10. 10

    C#对象不包含方法的定义

  11. 11

    错误'System.Diagnostics.Process'在Visual Studio 2008 C#中不包含'GetProcesses'的定义

  12. 12

    使用 C# 从 URL 获取内容后跟此错误“WebRequest 不包含 GetRespone 的定义和...”

  13. 13

    使用 C# 从 URL 获取内容得到这个错误“WebRequest 不包含 GetRespone 的定义和……”

  14. 14

    C ++成功编译,不包含<string>标头

  15. 15

    C ++成功编译,不包含<string>标头

  16. 16

    C#Roles.GetAllRoles()错误-不包含定义

  17. 17

    System.Array不包含“任何”的定义-C#

  18. 18

    CS1929 C#'RfcParameterClass'不包含'Cast'的定义

  19. 19

    Foreach语句不包含C#上GetEnumerator的公共定义

  20. 20

    dropdownlist控件asp.net C#错误为“ ASP.pract2_aspx”不包含“ dropDownList_Changed”的定义

  21. 21

    C#错误CS1061:类型“ System.Collections.Generic.List <int>”不包含“长度”的定义

  22. 22

    CS1061:类型“string”不包含“classChoice”的定义,也没有扩展方法“classChoice”

  23. 23

    List(of String())包含String()

  24. 24

    编辑不包含String的treeview的项目

  25. 25

    Linq错误:不包含“ InsertOnSubmit”的定义

  26. 26

    unity / vrtk不包含定义错误

  27. 27

    Visual Studio 2012错误“不包含定义”

  28. 28

    视图不包含 RemoveView 错误的定义

  29. 29

    C#自定义Dictionary <string,string>接受重复的键进行序列化

热门标签

归档