asp.net-mvc 返回新的 EmptyResult() VS 返回 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8561038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:38:36  来源:igfitidea点击:

return new EmptyResult() VS return NULL

asp.net-mvcasp.net-mvc-3action

提问by Artur Keyan

in ASP.NET MVC when my action will not return anything I use return new EmptyResult()or return null

在 ASP.NET MVC 中,当我的操作不会返回我使用的任何内容return new EmptyResult()return null

is there any difference?

有什么区别吗?

回答by dknaack

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

你可以返回null。MVC 将检测到并返回一个EmptyResult.

MSDN: EmptyResultrepresents a result that doesn't do anything, like a controller action returning null

MSDN:EmptyResult表示不执行任何操作的结果,例如返回 null 的控制器操作

Source code of MVC.

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 ControllerActionInvokerwhich shows if you return null, MVC will return EmptyResult.

来源ControllerActionInvoker显示,如果您返回 null,MVC 将返回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.

您可以在Codeplex上下载 Asp.Net MVC 项目的源代码。

回答by nemesv

When you return nullfrom an action the MVC framework (actually the ControllerActionInvokerclass) will internally create a new EmptyResult. So finally an instance of the EmptyResultclass will be used in both cases. So there is no real difference.

当您null从操作返回时,MVC 框架(实际上是ControllerActionInvoker类)将在内部创建一个新的EmptyResult. 因此,最终EmptyResult在这两种情况下都将使用该类的一个实例。所以没有真正的区别。

In my personal opinion return new EmptyResult()is better because it communicates more clearly that your action returns nothing.

我个人认为return new EmptyResult()更好,因为它更清楚地传达了您的行为没有任何回报。

回答by jim tollan

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.:

两者的作用基本相同,即 http 标头与空白页一起发送回。但是,如果您愿意,您可以进一步调整并返回一个具有适当 statusCode 和 statusDescription 的新 HttpStatusCodeResult()。IE:

var result = new HttpStatusCodeResult(999, "this didn't work as planned");
return result;

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:

[编辑]- 找到了一个很好的 HttpStatusCodeResult() 实现,它举例说明了如何在谷歌等中利用它:

http://weblogs.asp.net/gunnarpeipman/archive/2010/07/28/asp-net-mvc-3-using-httpstatuscoderesult.aspx

http://weblogs.asp.net/gunnarpeipman/archive/2010/07/28/asp-net-mvc-3-using-httpstatuscoderesult.aspx