C# 名称';HtmlEncoder';在当前上下文中不存在
我正在自学asp.net核心。我正在通过考试。在我的HelloWorldController中,我有以下方法: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
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上,它将向您显示可能的修复程序,如在线查找并安装此软件包的最新版本
希望这有助于修复