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

前端 未结 5 1473
刺人心
刺人心 2020-12-12 11:48

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

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

How can I

相关标签:
5条回答
  • 2020-12-12 12:14

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

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

    The StatusCode method returns a type of StatusCodeResult which implements IActionResult and can thus be used as a return type of your action.

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

    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

    0 讨论(0)
  • 2020-12-12 12:24
        [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 });
            }
        }
    
    0 讨论(0)
  • 2020-12-12 12:27

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

    0 讨论(0)
  • 2020-12-12 12:27

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

    public ActionResult SomeMethod(parameters...)
    {
       //others code here
       ...      
       Response.StatusCode = 200;
       return YourObject;  
    }
    
    0 讨论(0)
  • 2020-12-12 12:39

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

    [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
    }
    
    0 讨论(0)
提交回复
热议问题