ASP.Net MVC Controller Actions that return void

If I have the following controller action ...

public void DoSomething() { } 

will the wireframe actually convert it to this?

 public EmptyResult DoSomething() { return new EmptyResult(); } 
+62
asp.net-mvc
Jan 12 '10 at 11:50
source share
3 answers

Yes

The controller that returns void will create an EmptyResult.

Taken from

Life and Times of ASP.NET MVC Controller

+69
Jan 12 '10 at
source share

Looks like check out the source code for ControllerActionInvoker.cs . I have not tested it, but the logic tells me that returning void will set actionReturnValue to null, so EmptyResult is generated. This is the latest source code, did not check 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; } 
+3
Jan 12 '10 at
source share

It will not β€œconvert” it, but both will have the same effect as the user. The request will be sent, but the response will not be returned to the client.

Personally, I believe that you need to send some kind of response back to the client, even if you just write the continuation or success directly in the response stream. Even JSON true or an empty XML document is better than nothing.

+1
Jan 12
source share



All Articles