VS返回NULL返回新EmptyResult() [英] return new EmptyResult() VS return NULL

查看:446
本文介绍了VS返回NULL返回新EmptyResult()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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() or return null

is there any difference?

解决方案

You can return null. MVC will detect that and return a EmptyResult.

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 return EmptyResult.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆