如果我有以下控制器操作...
public void DoSomething()
{
}
这个框架会实际将它转换成这样吗?
public EmptyResult DoSomething()
{
return new EmptyResult();
}
public void DoSomething()
{
}
这个框架会实际将它转换成这样吗?
public EmptyResult DoSomething()
{
return new EmptyResult();
}
看起来是这样的,请查看ControllerActionInvoker.cs的源代码。我没有验证过,但逻辑告诉我,void返回将会将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文档也比什么都没有要好。