我希望下面的示例控制器返回一个状态码为418,不含任何内容。设置状态码很容易,但似乎还需要执行某些操作来表示请求结束。在ASP.NET Core之前的MVC或WebForms中,可能会调用Response.End()方法,但在ASP.NET Core中,该方法不存在,那么应该如何处理呢?
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();
}
}
StatusCode(418)
确实会返回内容。它返回一个包含状态码和其他几个属性的 JSON 对象,因此我不明白这甚至是一个有效的答案。 - Neutrino