我想要下面的示例控制器返回没有内容的状态代码418。设置状态代码很简单,但是似乎有一些需要做的事情来表明请求的结束。在MVC之前的ASP.NET Core或WebForms中,可能是对Response.End()的调用,但是如何在ASP.NET Core中响应.End不存在?
public class ExampleController : Controller
{
[HttpGet][Route("/example/main")]
public IActionResult Main()
{
this.HttpContext.Response.StatusCode = 418; //I'm a teapot
//How to end the request??????
//I don't actually want to return a view but perhaps the next
//line is required anyway?
return View();
}
}
解决方法
this.HttpContext.Response.StatusCode = 418; //I’m a teapot
How to end the request??????
尝试其他解决方案,只需:
return StatusCode(418);
您可以使用StatusCode(???)返回任何HTTP状态代码。
另外,您可以使用专用的结果:
成功:
> return Ok()< - Http状态码200
> return Created()< - Http状态码201
>返回NoContent(); < - Http状态码204
客户端错误:
> return BadRequest(); < - Http状态码400
>返回Unauthorized(); < - Http状态码401
>返回NotFound(); < - Http状态码404 更多细节:
> ControllerBase Class(谢谢@Technetium)
> StatusCodes.cs(ASP.NET Core中的常量)
> HTTP Status Codes on Wiki
> HTTP Status Codes IANA

