when it's a good idea to return "EmptyResult()" from a Controller
EmptyResult is used when you want to execute logic return inside the controller action method but does not want any result back to the view then EmptyResult return type is very important . It does not return any output to the browser. It shows the empty result set to the browser without adding the view.
@RobinMaben: No, null would not return an object from the method.
You would basically use it to signify that you are not doing anything with an action's result.
From MSDN:
Represents a result that does nothing, such as a controller action method that returns nothing.
I have personally used on actions defined in an AsyncController
, so if you have for instance an async action like:
public void SendMailAsync() { }
Basically an action in an AsnycController
, you'll need a xxxCompleted action as well (by convention)
public virtual ActionResult SendMailCompleted { // do whatever return new EmptyResult(); }
Since this is not an action meant to be called by a user but by a background task, I'm not going to do anything with the result anyway.
I've used it when creating RESTful web services. When doing a POST or DELETE operation, for example, the HTTP status code can convey enough info in itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With