in ASP.NET MVC when my action will not return anything I use return new EmptyResult()
or return null
is there any difference?
in ASP.NET MVC when my action will not return anything I use return new EmptyResult()
or return null
is there any difference?
You can return
null
. MVC will detect that and return aEmptyResult
.Source code of mvc.
And the source from
ControllerActionInvoker
wich shows if you return null, MVC will returnEmptyResult
.You can download the source code of the Asp.Net Mvc Project on Codeplex.
When you return
null
from an action the MVC framework (actually theControllerActionInvoker
class) will internally create a newEmptyResult
. So finally an instance of theEmptyResult
class will be used in both cases. So there is no real difference.In my personal opinion
return new EmptyResult()
is better because it communicates more clearly that your action returns nothing.Artur,
both do basically the same in that the http header is sent back along with a blank page. you could however, tweak that further if you wished and return a new HttpStatusCodeResult() with the appropriate statusCode and statusDescription. i.e.:
I think that may be a useful alternative.
[edit] - found a nice implementation of HttpStatusCodeResult() which exemplifies how to leverage this with google etc in mind:
http://weblogs.asp.net/gunnarpeipman/archive/2010/07/28/asp-net-mvc-3-using-httpstatuscoderesult.aspx