SF Developer
SF Developer

Reputation: 5384

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

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

Upvotes: 37

Views: 34330

Answers (7)

silkfire
silkfire

Reputation: 25983

For a .NET Core solution (tested in v2.1), use:

return StatusCode(StatusCodes.Status204NoContent);

This will return a StatusCodeResult with the appropriate HTTP status code 204 No Content.

Upvotes: 4

KamalDeep
KamalDeep

Reputation: 834

The EmptyResult is a class in MVC which does not return anything, like Void method .

EmptyResult is used when you want to execute logic written 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 require to add the view.

Upvotes: 1

Jake Kalstad
Jake Kalstad

Reputation: 2065

When the ajax performs an action that doesn't need reflection/confirmation on the UI.

Upvotes: 6

Sergi Papaseit
Sergi Papaseit

Reputation: 16194

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.

Upvotes: 40

Gup3rSuR4c
Gup3rSuR4c

Reputation: 9490

I'm assuming its the same as doing return (null) in the action. It could be useful at times. I've used it to Response.Write info to the output while debugging, but didn't need the remainder of the view rendered.

When you use it, you'll get a blank white page with nothing on it, unless you output something of your own.

Upvotes: 4

dommer
dommer

Reputation: 19820

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.

Upvotes: 10

Andrei Andrushkevich
Andrei Andrushkevich

Reputation: 9973

for example: You can return empty result with status 401.

Upvotes: 3

Related Questions