72

If I have the following controller action...

public void DoSomething()
{
}

will the framework actually convert it to this?

public EmptyResult DoSomething()
{
  return new EmptyResult();
}
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Chris Arnold
  • 5,753
  • 5
  • 35
  • 55

3 Answers3

81

Yes

A controller that returns void will produce an EmptyResult.

Taken from

The Life And Times of an ASP.NET MVC Controller

Martijn Laarman
  • 13,476
  • 44
  • 63
3

Seems so, check the source code of ControllerActionInvoker.cs. I haven't verified it, but logic tells me that a void return will set actionReturnValue to null, so an EmptyResult is generated. This is the most recent source code, haven't checked the source for ASP.net MVC 1.0.

protected virtual ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue) {
    if (actionReturnValue == null) {
        return new EmptyResult();
    }

    ActionResult actionResult = (actionReturnValue as ActionResult) ??
        new ContentResult { Content = Convert.ToString(actionReturnValue, CultureInfo.InvariantCulture) };
    return actionResult;
}
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
1

It won't "convert" it, but the two would have the same effect as far as the user is concern. A request would be sent, but no response would come back to the client.

Personally, I think you need to send some response back to the client, even if you just write a continue or success directly to the response stream. Even a JSON true, or an empty XML document is better than nothing at all.

Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
  • 1
    Why the downvotes here? Countered in the name of reason; very good point about always returning something. – ProfK Apr 14 '12 at 04:55
  • 9
    I suspect the reason for the downvote (I didn't) is because a response "does" come back. A 200 OK response. The response body is empty, though. I think with ajax operations it is reasonable to use HTTP status codes to communicate operation results. 200 OK, 500 Error, 403 forbidden, etc. A body isn't always necessary. – MarkPflug Jul 01 '14 at 00:03