<legend id='IVQLO'><style id='IVQLO'><dir id='IVQLO'><q id='IVQLO'></q></dir></style></legend>

      <i id='IVQLO'><tr id='IVQLO'><dt id='IVQLO'><q id='IVQLO'><span id='IVQLO'><b id='IVQLO'><form id='IVQLO'><ins id='IVQLO'></ins><ul id='IVQLO'></ul><sub id='IVQLO'></sub></form><legend id='IVQLO'></legend><bdo id='IVQLO'><pre id='IVQLO'><center id='IVQLO'></center></pre></bdo></b><th id='IVQLO'></th></span></q></dt></tr></i><div id='IVQLO'><tfoot id='IVQLO'></tfoot><dl id='IVQLO'><fieldset id='IVQLO'></fieldset></dl></div>
      • <bdo id='IVQLO'></bdo><ul id='IVQLO'></ul>

      1. <small id='IVQLO'></small><noframes id='IVQLO'>

        <tfoot id='IVQLO'></tfoot>

        如何从控制器返回特定的状态码并且没有内容?

        时间:2023-06-08

      2. <legend id='6iNOz'><style id='6iNOz'><dir id='6iNOz'><q id='6iNOz'></q></dir></style></legend>
        <tfoot id='6iNOz'></tfoot>
          <tbody id='6iNOz'></tbody>

          <small id='6iNOz'></small><noframes id='6iNOz'>

            <bdo id='6iNOz'></bdo><ul id='6iNOz'></ul>
              <i id='6iNOz'><tr id='6iNOz'><dt id='6iNOz'><q id='6iNOz'><span id='6iNOz'><b id='6iNOz'><form id='6iNOz'><ins id='6iNOz'></ins><ul id='6iNOz'></ul><sub id='6iNOz'></sub></form><legend id='6iNOz'></legend><bdo id='6iNOz'><pre id='6iNOz'><center id='6iNOz'></center></pre></bdo></b><th id='6iNOz'></th></span></q></dt></tr></i><div id='6iNOz'><tfoot id='6iNOz'></tfoot><dl id='6iNOz'><fieldset id='6iNOz'></fieldset></dl></div>
                • 本文介绍了如何从控制器返回特定的状态码并且没有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望下面的示例控制器返回没有内容的状态代码 418.设置状态码很容易,但似乎需要做一些事情来表示请求的结束.在 ASP.NET Core 之前的 MVC 或 WebForms 中,这可能是对 Response.End() 的调用,但它在 Response.End 的 ASP.NET Core 中如何工作不存在?

                  I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs to be done to signal the end of the request. In MVC prior to ASP.NET Core or in WebForms that might be a call to Response.End() but how does it work in ASP.NET Core where Response.End does not exist?

                  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;//我是茶壶

                  如何结束请求?

                  尝试其他解决方案,只需:

                  Try other solution, just:

                  return StatusCode(418);
                  


                  您可以使用 StatusCode(???) 来返回任何 HTTP 状态代码.


                  You could use StatusCode(???) to return any HTTP status code.


                  此外,您可以使用专用结果:


                  Also, you can use dedicated results:

                  成功:

                  • return Ok() ← Http状态码200
                  • return Created() ← Http状态码201
                  • return NoContent(); ← Http状态码204
                  • return Ok() ← Http status code 200
                  • return Created() ← Http status code 201
                  • return NoContent(); ← Http status code 204

                  客户端错误:

                  • return BadRequest(); ← Http状态码400
                  • return Unauthorized(); ← Http状态码401
                  • return NotFound(); ← Http状态码404
                  • return BadRequest(); ← Http status code 400
                  • return Unauthorized(); ← Http status code 401
                  • return NotFound(); ← Http status code 404


                  更多细节:

                  • ControllerBase 类(感谢@Technetium)
                  • StatusCodes.cs(在 ASP.NET Core 中可用的常量)
                  • Wiki 上的 HTTP 状态代码
                  • HTTP 状态代码 IANA
                  • ControllerBase Class (Thanks @Technetium)
                  • StatusCodes.cs (consts aviable in ASP.NET Core)
                  • HTTP Status Codes on Wiki
                  • HTTP Status Codes IANA

                  这篇关于如何从控制器返回特定的状态码并且没有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 asp.net core 中上传文件? 下一篇:如何将值更新到 appsetting.json?

                  相关文章

                • <i id='xOj9k'><tr id='xOj9k'><dt id='xOj9k'><q id='xOj9k'><span id='xOj9k'><b id='xOj9k'><form id='xOj9k'><ins id='xOj9k'></ins><ul id='xOj9k'></ul><sub id='xOj9k'></sub></form><legend id='xOj9k'></legend><bdo id='xOj9k'><pre id='xOj9k'><center id='xOj9k'></center></pre></bdo></b><th id='xOj9k'></th></span></q></dt></tr></i><div id='xOj9k'><tfoot id='xOj9k'></tfoot><dl id='xOj9k'><fieldset id='xOj9k'></fieldset></dl></div>

                  1. <tfoot id='xOj9k'></tfoot>

                    • <bdo id='xOj9k'></bdo><ul id='xOj9k'></ul>
                    <legend id='xOj9k'><style id='xOj9k'><dir id='xOj9k'><q id='xOj9k'></q></dir></style></legend>

                      <small id='xOj9k'></small><noframes id='xOj9k'>