【发布时间】:2010-01-12 11:50:23
【问题描述】:
如果我有以下控制器操作...
public void DoSomething()
{
}
框架会真正将它转换成这个吗?
public EmptyResult DoSomething()
{
return new EmptyResult();
}
【问题讨论】:
标签: asp.net asp.net-mvc
如果我有以下控制器操作...
public void DoSomething()
{
}
框架会真正将它转换成这个吗?
public EmptyResult DoSomething()
{
return new EmptyResult();
}
【问题讨论】:
标签: asp.net asp.net-mvc
【讨论】:
好像是这样,检查source code of ControllerActionInvoker.cs。我还没有验证它,但逻辑告诉我,一个 void return 会将 actionReturnValue 设置为 null,因此会生成一个 EmptyResult。这是最新的源代码,没有查过ASP.net MVC 1.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;
}
【讨论】:
它不会“转换”它,但就用户而言,两者的效果相同。将发送一个请求,但不会向客户端返回任何响应。
就个人而言,我认为您需要将一些响应发送回客户端,即使您只是将继续或成功直接写入响应流。即使是 JSON true,或者一个空的 XML 文档也比什么都没有好。
【讨论】: