VS返回NULL返回新EmptyResult() [英] return new EmptyResult() VS return NULL
问题描述
在ASP.NET MVC的时候我的动作将不会返回任何东西我用返回新EmptyResult()
或返回NULL
有什么区别呢?
您可以返回空
。 MVC将检测并返回 EmptyResult
。
MSDN: EmptyResult 重新presents的结果,这并不做任何事情,
像一个控制器动作返回null
块引用>MVC的来源$ C $ C。
公共类EmptyResult:{的ActionResult 私人静态只读EmptyResult _singleton =新EmptyResult(); 内部静态EmptyResult实例{
获得{
返回_singleton;
}
} 公共覆盖无效的ExecuteReuslt(ControllerContext上下文){
}
}和从
ControllerActionInvoker
至极源显示,如果你返回null,
MVC将返回EmptyResult
。受保护的虚拟的ActionResult CreateActionResult(ControllerContext controllerContext,ActionDescriptor actionDescriptor,对象actionReturnValue){
如果(actionReturnValue == NULL){
返回新EmptyResult();
} 的ActionResult的ActionResult =(actionReturnValue为的ActionResult)?
新ContentResult类型{内容= Convert.ToString(actionReturnValue,CultureInfo.InvariantCulture)};
返回的ActionResult;
}您也可以下载Asp.Net MVC项目的源$ C $ C在 codePLEX 。
in ASP.NET MVC when my action will not return anything I use
return new EmptyResult()
orreturn null
is there any difference?
解决方案You can return
null
. MVC will detect that and return aEmptyResult
.MSDN: EmptyResult represents a result that doesn't do anything, like a controller action returning null
Source code of mvc.
public class EmptyResult : ActionResult { private static readonly EmptyResult _singleton = new EmptyResult(); internal static EmptyResult Instance { get { return _singleton; } } public override void ExecuteResult(ControllerContext context) { } }
And the source from
ControllerActionInvoker
wich shows if you return null, MVC will returnEmptyResult
.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; }
You can download the source code of the Asp.Net Mvc Project on Codeplex.
这篇关于VS返回NULL返回新EmptyResult()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!