Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称';HtmlEncoder';在当前上下文中不存在_C#_Asp.net Core - Fatal编程技术网

C# 名称';HtmlEncoder';在当前上下文中不存在

C# 名称';HtmlEncoder';在当前上下文中不存在,c#,asp.net-core,C#,Asp.net Core,我正在自学asp.net核心。我正在通过考试。在我的HelloWorldController中,我有以下方法: public string Welcome(string name, int numTimes = 1) { return HtmlEncoder.Default.Encode($"Hello {name}, numTimes: {numTimes}"); } 但我得到了一个错误: Severity Code Description Project File Line Sup

我正在自学asp.net核心。我正在通过考试。在我的HelloWorldController中,我有以下方法:

public string Welcome(string name, int numTimes = 1)
{
    return HtmlEncoder.Default.Encode($"Hello {name}, numTimes: {numTimes}");
}
但我得到了一个错误:

Severity Code Description Project File Line Suppression State
Error    CS0103    The name 'HtmlEncoder' does not exist in the current context    TestApp..NETCoreApp,Version=v1.0    c:\Users\administrator\documents\visual studio 2015\Projects\TestApp\src\TestApp\Controllers\HelloWorldController.cs    23    Active

我做错了什么?

在HelloWorldController中添加以下行:

using System.Text.Encodings.Web;
或者您可以替换行
return HtmlEncoder.Default.Encode($“Hello{name},numTimes:{numTimes}”)使用此行:

return System.Text.Encodings.Web.HtmlEncoder.Default.Encode($"Hello {name}, numTimes: {numTimes}");

您需要安装编码器包。将光标悬停在返回语句中的HtmlEncoder上,它将向您显示可能的修复程序,如在线查找并安装此软件包的最新版本

希望这有助于修复