【问题标题】:How to return a 200 HTTP Status Code from ASP.NET MVC 3 controller如何从 ASP.NET MVC 3 控制器返回 200 HTTP 状态代码
【发布时间】:2011-07-01 15:55:08
【问题描述】:

我正在编写一个从第三方服务接受 POST 数据的应用程序。

发布此数据后,我必须返回 200 HTTP 状态代码。

如何从我的控制器执行此操作?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    在您的控制器中,您将返回一个像这样的 HttpStatusCodeResult...

    [HttpPost]
    public ActionResult SomeMethod(...your method parameters go here...)
    {
       // todo: put your processing code here
    
       //If not using MVC5
       return new HttpStatusCodeResult(200);
    
       //If using MVC5
       return new HttpStatusCodeResult(HttpStatusCode.OK);  // OK = 200
    }
    

    【讨论】:

    • 或者更确切地说“返回新的 HttpStatusCodeResult((int)HttpStatusCode.OK);”
    • @dan,这不是必需的。有采用intHttpStatusCode 的重载。
    • 要返回 204 状态码,请执行以下操作:return new HttpStatusCodeResult(HttpStatusCode.NoContent);
    • @MEMark,我必须进行投射才能使其正常工作。使用 .NET 4 和 MVC 3 时,我没有获得需要 HttpStatusCode 的覆盖。
    • @ShawnSouth,我似乎无法在文档中找到有关哪些版本包含此重载的任何信息。 msdn.microsoft.com/en-us/library/hh413957(v=vs.118).aspx
    【解决方案2】:

    200 只是成功请求的正常 HTTP 标头。如果这就是您所需要的全部,只需使用控制器return new EmptyResult();

    【讨论】:

    • 您应该改用HttpStatusCodeResult(...),因为它更明确地说明了您要实现的目标。根据接受的答案。
    【解决方案3】:

    您可以简单地将响应的状态码设置为 200,如下所示

    public ActionResult SomeMethod(parameters...)
    {
       //others code here
       ...      
       Response.StatusCode = 200;
       return YourObject;  
    }
    

    【讨论】:

    • 支持,因为这允许您发送回其他信息以及状态代码
    【解决方案4】:
        [HttpPost]
        public JsonResult ContactAdd(ContactViewModel contactViewModel)
        {
            if (ModelState.IsValid)
            {
                var job = new Job { Contact = new Contact() };
    
                Mapper.Map(contactViewModel, job);
                Mapper.Map(contactViewModel, job.Contact);
    
                _db.Jobs.Add(job);
    
                _db.SaveChanges();
    
                //you do not even need this line of code,200 is the default for ASP.NET MVC as long as no exceptions were thrown
                //Response.StatusCode = (int)HttpStatusCode.OK;
    
                return Json(new { jobId = job.JobId });
            }
            else
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(new { jobId = -1 });
            }
        }
    

    【讨论】:

    • 正是我的用例,返回 Json 对象但也想提供 HTTP_STATUS_CODE
    • 对于返回状态码的 WebAPI,使用:new StatusCodeResult(HttpStatusCode.NotModified, Request);
    • 结合所有用例的最佳答案
    【解决方案5】:

    在 .NET Core 中执行此操作的方法(在撰写本文时)如下:

    public async Task<IActionResult> YourAction(YourModel model)
    {
        if (ModelState.IsValid)
        {
            return StatusCode(200);
        }
    
        return StatusCode(400);
    }
    

    StatusCode 方法返回一种实现 IActionResultStatusCodeResult 类型,因此可以用作您的操作的返回类型。 p>

    作为重构,您可以通过使用 HTTP 状态代码枚举类型来提高可读性,例如:

    return StatusCode((int)HttpStatusCode.OK);
    

    此外,您还可以使用一些内置的结果类型。例如:

    return Ok(); // returns a 200
    return BadRequest(ModelState); // returns a 400 with the ModelState as JSON
    

    参考。 StatusCodeResult - https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.statuscoderesult?view=aspnetcore-2.1

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 2022-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 2018-05-03
      • 1970-01-01
      • 2011-12-30
      相关资源
      最近更新 更多