这是从ASP.NET Web API返回HTML的后续内容.
我按照说明操作,但我在浏览器中收到错误406.我的代码:
[Produces("text/html")]
[Route("api/[controller]")]
public class AboutController : Controller
{
[HttpGet]
public string Get()
{
return "<html><body>Welcome</body></html>";
}
...
Run Code Online (Sandbox Code Playgroud)
而且,简单地说:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
Run Code Online (Sandbox Code Playgroud)
当我删除该Produces行时,我<html><body>Welcome</body></html>在浏览器中获得纯文本(没有错误).
我错过了什么?谢谢.
Fra*_*roe 23
从AspNetCore 2.0开始,建议使用
ContentResult而不是Produce属性
解决方案是:
[HttpGet]
public ContentResult Get()
{
return new ContentResult {
ContentType = "text/html",
StatusCode = (int) HttpStatusCode.OK,
Content = "<html><body>Welcome</body></html>"
};
}
Run Code Online (Sandbox Code Playgroud)
没有必要改变AddMvc(Produce当然没有属性).
我希望这可以帮助别人.
| 归档时间: |
|
| 查看次数: |
4446 次 |
| 最近记录: |