아래 예제 컨트롤러가 내용이없는 상태 코드 418을 반환하도록합니다. 상태 코드를 설정하는 것은 쉽지만 요청이 끝났음을 알리기 위해해야 할 일이있는 것처럼 보입니다. Response.End()
에 대한 호출 일 수있는 ASP.NET Core 또는 WebForms 이전의 MVC에서 Response.End
가없는 ASP.NET 코어에서 어떻게 작동합니까?
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
어떻게 요청을 끝내시겠습니까?
다른 솔루션을 사용해보십시오.
return StatusCode(418);
StatusCode(???)
을 사용하여 HTTP 상태 코드를 반환 할 수 있습니다.
또한 전용 결과를 사용할 수 있습니다.
성공 :
return Ok()
← HTTP 상태 코드 200return Created()
← HTTP 상태 코드 201return NoContent();
← HTTP 상태 코드 204클라이언트 오류 :
return BadRequest();
← HTTP 상태 코드 400return Unauthorized();
← HTTP 상태 코드 401return NotFound();
← HTTP 상태 코드 404
자세한 내용 :
IHttpActionResult
로이 작업을 수행하려는 사람이 웹 API 프로젝트에있을 수 있으면 아래가 도움이 될 수 있습니다.
// GET: api/Default/
public IHttpActionResult Get()
{
//return Ok();//200
//return StatusCode(HttpStatusCode.Accepted);//202
//return BadRequest();//400
//return InternalServerError();//500
//return Unauthorized();//401
return Ok();
}
이 코드는 비 .NET Core MVC 컨트롤러에서 작동합니다.
this.HttpContext.Response.StatusCode = 418; // I'm a teapot
return Json(new { status = "mer" }, JsonRequestBehavior.AllowGet);