2016-09-27 53 views
2

我在教自己的asp.net內核。我正在通過getting started guide。在我HelloWorldController我有這樣的方法:當前上下文中不存在名稱'HtmlEncoder'

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 

我做了什麼錯?

回答

1

添加下面一行到你的HelloWorldController:

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

using System.Text.Encodings.Web; 

或者你可以用下面這行替換行return HtmlEncoder.Default.Encode($"Hello {name}, numTimes: {numTimes}");

相關問題