这是对Return HTML from ASP.NET Web API的后续。
我按照说明进行操作,但在浏览器中收到错误406。
我的代码:
[Produces("text/html")]
[Route("api/[controller]")]
public class AboutController : Controller
{
[HttpGet]
public string Get()
{
return "<html><body>Welcome</body></html>";
}
...
并且,简单地:public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
当我删除Produces
行时,我在浏览器中得到纯文本<html><body>Welcome</body></html>
(没有错误)。我想念什么?谢谢。
最佳答案
[HttpGet]
public ContentResult Get()
{
return new ContentResult {
ContentType = "text/html",
StatusCode = (int) HttpStatusCode.OK,
Content = "<html><body>Welcome</body></html>"
};
}
无需更改
AddMvc
(当然也没有Produce
属性)。我希望这可以帮助别人。
关于c# - 从ASP.NET Web API ASP.NET Core 2返回HTML并获得http状态406,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46793783/