Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 = >> return EmptyResult() When it's a good idea to return this?

when it's a good idea to return "EmptyResult()" from a Controller

like image 484
SF Developer Avatar asked Mar 27 '11 20:03

SF Developer


People also ask

How do I return EmptyResult?

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.

Can we return blank view in MVC?

@RobinMaben: No, null would not return an object from the method.


2 Answers

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.

like image 161
Sergi Papaseit Avatar answered Oct 27 '22 20:10

Sergi Papaseit


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.

like image 20
dommer Avatar answered Oct 27 '22 19:10

dommer