ASP.Net MVC Controller Actions that return void

前端 未结 3 712
忘了有多久
忘了有多久 2020-12-03 06:26

If I have the following controller action...

public void DoSomething()
{
}

will the framework actually convert it to this?

         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 06:49

    Seems so, check the source code of ControllerActionInvoker.cs. I haven't verified it, but logic tells me that a void return will set actionReturnValue to null, so an EmptyResult is generated. This is the most recent source code, haven't checked the source for 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;
    }
    

提交回复
热议问题