asp.net-mvc 如何从 ASP.NET MVC 3 控制器返回 200 HTTP 状态代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5072804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:55:25  来源:igfitidea点击:

How to return a 200 HTTP Status Code from ASP.NET MVC 3 controller

asp.net-mvc

提问by Paul Brown

I am writing an application that is accepting POST data from a third party service.

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

When this data is POSTed I must return a 200 HTTP Status Code.

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

How can I do this from my controller?

我怎样才能从我的控制器做到这一点?

回答by Brian Behm

In your controller you'd return an HttpStatusCodeResult like this...

在你的控制器中,你会像这样返回一个 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
}

回答by Kevin Stricker

200 is just the normal HTTP header for a successful request. If that's allyou need, just have the controller return new EmptyResult();

200 只是成功请求的正常 HTTP 标头。如果这一切你所需要的,只是有控制器return new EmptyResult();

回答by Hyman

You can simply set the status code of the response to 200 like the following

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

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

回答by Brian Ogden

    [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 });
        }
    }

回答by user1477388

The way to do this in .NET Core is (at the time of writing) as follows:

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

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

    return StatusCode(400);
}

The StatusCodemethod returns a type of StatusCodeResultwhich implements IActionResultand can thus be used as a return type of your action.

所述的StatusCode方法返回的类型StatusCodeResult它实现IActionResult并且因此可以用作您的动作的返回类型。

As a refactor, you could improve readability by using a cast of the HTTP status codes enum like:

作为重构,您可以通过使用 HTTP 状态代码枚举的强制转换来提高可读性,例如:

return StatusCode((int)HttpStatusCode.OK);

Furthermore, you could also use some of the built in result types. For example:

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

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

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

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