如果我有下面的控制器操作..。
public void DoSomething() { }
框架真的会把它转换成这样吗?
public EmptyResult DoSomething() { return new EmptyResult(); }
它不会“转换”它,但是对于用户来说,两者会有相同的效果。将发送一个请求,但是不会有任何响应返回到客户端。
就个人而言,我认为您需要向客户端发送一些响应,即使您只是直接向响应流编写了一个“继续”或“成功”。即使是一个 JSON true,或者一个空的 XML 文档也比什么都没有强。
true
看起来是这样,检查 Controlleractioninvoker.cs 的源代码。我还没有验证它,但是逻辑告诉我 void 返回将把 actionReturn 值设置为 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; }
是的
返回 void 的控制器将生成 EmptyResult。
从
ASP.NET MVC 控制器的生命周期