264

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?

7 Answers 7

459

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
}
Sign up to request clarification or add additional context in comments.

12 Comments

or rather "return new HttpStatusCodeResult((int)HttpStatusCode.OK);"
@dan, that's not needed. There are overloads that take an int as well as an HttpStatusCode.
to return a 204 status code do this: return new HttpStatusCodeResult(HttpStatusCode.NoContent);
@MEMark, I had to cast to make it work. Using .NET 4 & MVC 3 I was not provided with an override that would take an HttpStatusCode.
@ShawnSouth, I can't seem to find any information in the docs on what versions contain this overload. msdn.microsoft.com/en-us/library/hh413957(v=vs.118).aspx
|
58

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

1 Comment

You should use HttpStatusCodeResult(...) instead as it is much more explicit about what you are trying to achieve. As per the accepted answer.
56

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

1 Comment

Upvote because this allows you to send back other info as well as just the status code
29
    [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 });
        }
    }

3 Comments

Exactly my use case, returning Json object but also wanted to supply HTTP_STATUS_CODE
For WebAPI returning status codes, use: new StatusCodeResult(HttpStatusCode.NotModified, Request);
Best answer as it combines all use cases
15

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://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.statuscoderesult?view=aspnetcore-2.1

Comments

1

If you have a custom object that needs to be returned within the Status 200 response, you can pass a custom object as an OkObjectResult:

public IActionResult Get()
    {
        CustomObject customObject = new CustomObject();

        return Ok(customObject);
    }

Comments

0

In project root folder I was deleted bin folder then rebuild project again. This worked for me:

D:\C# Projects\my_demo_project\WebApplication1\bin

Good luck!

1 Comment

I think your answer was added to the wrong question??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.