哪个返回类型用于可以返回匿名类型的操作方法,还有HTTP状态代码?

时间:2016-02-24 15:20:44

标签: c# asp.net asp.net-mvc asp.net-core

我看到有多种方法可以在Asp.Net Core控制器中返回数据时可能出现各种结果:

1)object

[HttpPost]
public object RefreshToken()
{
    if (Validate())
    {
        return new
               {
                   token = CreateToken()
               };
    }

    return HttpUnauthorized()
}

2)dynamic

[HttpPost]
public dynamic RefreshToken()
{
    if (Validate())
    {
        return new
               {
                   token = CreateToken()
               };
    }

    return HttpUnauthorized()
}

3)IActionResult

[HttpPost]
public IActionResult RefreshToken()
{
    if (Validate())
    {
                return new ObjectResult(new
                {
                    token = CreateToken()
                });
    }

    return HttpUnauthorized()
}

这三种方法有什么不同吗?应该首选哪一个?

1 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,问题主要是关于编写程序的风格,因为所有的选项都会以相同的方式调用。

MVC以相同的方式执行所有操作。 invocationResult = actionMethodInfo.Invoke(instance, orderedActionArguments); !!! {我将public static Task<object> ExecuteAsync( MethodInfo actionMethodInfo, object instance, object[] orderedActionArguments) { object invocationResult = null; try { invocationResult = actionMethodInfo.Invoke(instance, orderedActionArguments); // !!! } catch (TargetInvocationException targetInvocationException) { // Capturing the exception and the original callstack and rethrow for external exception handlers. var exceptionDispatchInfo = ExceptionDispatchInfo.Capture(targetInvocationException.InnerException); exceptionDispatchInfo.Throw(); } return CoerceResultToTaskAsync( invocationResult, actionMethodInfo.ReturnType, actionMethodInfo.Name, actionMethodInfo.DeclaringType); } 置于评论中)调用控制器操作:

object

结果将始终解释为object invocationResultCoerceResultToTaskAsync)。然后方法Task测试返回对象的类型是否为var actionReturnValue = await ControllerActionExecutor.ExecuteAsync(...);,如果不是,则将其转换为Task。然后外部方法(CreateActionResult内部)返回The linevar actionResult = actionReturnValue as IActionResult; if (actionResult != null) { return actionResult; })中的值hereIActionResult测试,返回的结果为new ObjectResult(...)接口。如果没有,则返回internal static IActionResult CreateActionResult(Type declaredReturnType, object actionReturnValue) { if (declaredReturnType == null) { throw new ArgumentNullException(nameof(declaredReturnType)); } // optimize common path var actionResult = actionReturnValue as IActionResult; if (actionResult != null) { return actionResult; } if (declaredReturnType == typeof(void) || declaredReturnType == typeof(Task)) { return new EmptyResult(); } // Unwrap potential Task<T> types. var actualReturnType = GetTaskInnerTypeOrNull(declaredReturnType) ?? declaredReturnType; if (actionReturnValue == null && typeof(IActionResult).GetTypeInfo().IsAssignableFrom(actualReturnType.GetTypeInfo())) { throw new InvalidOperationException( Resources.FormatActionResult_ActionReturnValueCannotBeNull(actualReturnType)); } return new ObjectResult(actionReturnValue) { DeclaredType = actualReturnType }; } 。请参阅下面的代码副本

object

换句话说,所有代码都是一样的。第一个选项返回ObjectResult,最后一个选项将完全相同。我个人更愿意使用第一个选项没有明确调用IActionResult,但在其他情况下,element = driver.switch_to.active_element 的最后一个版本足够可读。这更符合品味。