首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >返回空的ASP.Net MVC控制器操作

返回空的ASP.Net MVC控制器操作
EN

Stack Overflow用户
提问于 2010-01-12 19:50:23
回答 2查看 47.6K关注 0票数 71

如果我有下面的控制器动作...

public void DoSomething()
{
}

框架真的会把它转换成这样吗?

public EmptyResult DoSomething()
{
  return new EmptyResult();
}
EN

回答 2

Stack Overflow用户

发布于 2010-01-12 19:56:27

看起来是这样,检查一下source code of ControllerActionInvoker.cs。我还没有验证它,但是逻辑告诉我,一个空的返回值会将actionReturnValue设置为null,因此会生成一个EmptyResult。这是最新的源代码,还没有检查过ASP.net MVC1.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
EN

Stack Overflow用户

发布于 2010-01-12 19:54:50

它不会“转换”它,但对于用户而言,这两种方法的效果是一样的。将发送一个请求,但不会向客户端返回任何响应。

就我个人而言,我认为您需要向客户端发送一些响应,即使您只是直接向响应流编写了continue或success。即使是一个JSON,或者一个空的true文档也比什么都没有要好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2048643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档