Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC Controller Actions that return void

If I have the following controller action...

public void DoSomething() { } 

will the framework actually convert it to this?

public EmptyResult DoSomething() {   return new EmptyResult(); } 
like image 354
Chris Arnold Avatar asked Jan 12 '10 11:01

Chris Arnold


People also ask

CAN controller return void?

A controller that returns void will produce an EmptyResult. OK, so it's using a Null Object Pattern.

Can a action method be void?

There is nothing like a VOID ActionResult (Action Method), hence ASP.Net MVC has created a class EmptyResult whose object is returned in case when NULL value or Nothing needs to be returned from Controller to View in ASP.Net MVC Razor.

Which type of objects do actions return?

The action method can return primitive type like int, string or complex type like List etc.


2 Answers

Yes

A controller that returns void will produce an EmptyResult.

Taken from

The Life And Times of an ASP.NET MVC Controller

like image 156
Martijn Laarman Avatar answered Oct 13 '22 03:10

Martijn Laarman


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; } 
like image 38
Michael Stum Avatar answered Oct 13 '22 03:10

Michael Stum