Best way to do an Action that does not return data

7

I am making an ajax call to change an object. The call does not have to return anything. Only the code http 200 (OK) or 500 (error). I'm using the following code in the action but I do not know if it's the best:

return new EmptyResult();

What is the best way to do an Action that does not return data?

    
asked by anonymous 16.01.2015 / 01:54

2 answers

4

It is using ActionResult itself. The problem is that EmptyResult does not return anything even , nor a request code:

public ActionResult MinhaActionSucesso()
{    
   // Antes do MVC5
   return new HttpStatusCodeResult(200);

   // MVC5+
   return new HttpStatusCodeResult(HttpStatusCode.OK);
}

public ActionResult MinhaActionErroInterno()
{    
   // Antes do MVC5
   return new HttpStatusCodeResult(500);

   // MVC5+
   return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}

See more about the HttpStatusCode enumerations here .

See more about HttpStatusCodeResult here .

    
16.01.2015 / 05:04
3

For ASP.NET MVC the EmptyResult works fine. But if the function of your action is only to be called by AJAX (ie, basically an API), then you can also use the ApiController (ASP.NET Web API), and in your action you return this.Ok() or this.InternalServerError() (the return type of the method would be IHttpActionResult ), making it clearer:

public class TestController : ApiController
{
    public IHttpActionResult Get()
    {
        if (algumaCondicaoBoa)
        {
            return this.Ok();
        }
        else
        {
            return this.InternalServerError();
        }
    }
}
    
16.01.2015 / 03:10