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

asp.net Mvc

asp.net Mvc Problem Overview


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 do this from my controller?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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
}

Solution 2 - asp.net Mvc

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

Solution 3 - asp.net Mvc

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

Solution 4 - asp.net Mvc

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

Solution 5 - asp.net Mvc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul BrownView Question on Stackoverflow
Solution 1 - asp.net MvcBrian BehmView Answer on Stackoverflow
Solution 2 - asp.net MvcKevin StrickerView Answer on Stackoverflow
Solution 3 - asp.net MvcJackView Answer on Stackoverflow
Solution 4 - asp.net MvcBrian OgdenView Answer on Stackoverflow
Solution 5 - asp.net Mvcuser1477388View Answer on Stackoverflow