在ASP.NET MVC中显式指定控制器返回的ActionResult类型是一种好习惯

时间:2022-06-05 04:02:53

I've been using ASP.NET MVC for a little while now and seem to find myself constantly returning things other than ActionResult from my controllers. I obviously return ViewResults but also JSonResults and also a couple of custom results that we've built in house.

我一直在使用ASP.NET MVC一段时间,似乎发现自己经常从我的控制器返回ActionResult以外的东西。我显然返回了ViewResults,还有JSonResults以及我们内部构建的一些自定义结果。

I'm wondering tho, if, instead of declaring my controller methods like:

我想知道,如果,而不是声明我的控制器方法,如:

public ActionResult Index()

I should start declaring them as

我应该开始宣布它们为

public ViewResult Index()

or

要么

public JsonResult Search()

if I always know that the Index action on my controller will always return a ViewResult or the Search action on my controller will always return a JsonResult?

如果我总是知道我的控制器上的索引操作将始终返回ViewResult,或者我的控制器上的搜索操作将始终返回JsonResult?

EDIT: Just to clarify, I'm talking specifically about situations where I will always want a specific type of ActionResult to be returned.

编辑:只是为了澄清,我正在特别谈论我总是希望返回特定类型的ActionResult的情况。

3 个解决方案

#1


6  

I vote yes for two reasons.

我投赞成票有两个原因。

  1. You are explicitly declaring what you expect the method to return and the compiler will catch any attempt to do otherwise. No need for a unit test that does a Assert( result is ViewResult).

    您明确声明了您希望该方法返回的内容,编译器将捕获任何其他尝试。不需要进行Assert的单元测试(结果是ViewResult)。

  2. You have to cast the result to the expected type in your tests when examining any properties unique to that result type (For example, checking Url property of the RedirectResult). Simply declaring the test variable as var removes any brittleness incurred by changing types.

    在检查该结果类型的唯一属性时,必须将结果转换为测试中的预期类型(例如,检查RedirectResult的Url属性)。简单地将测试变量声明为var可以消除因更改类型而导致的任何脆弱性。

#2


2  

By declaring a more specific return type you're gaining a little bit more of compiler type checking that you now don't have to cover in unit tests.

通过声明更具体的返回类型,您可以获得更多的编译器类型检查,您现在不必在单元测试中进行覆盖。

You would be binding yourself, however, to that type, and would have to revert if that changes. A common example is if you have to redirect the user elsewhere on some special conditions by returning a RedirectResult.

但是,您将自己绑定到该类型,并且如果更改则必须还原。一个常见的例子是,如果必须通过返回RedirectResult将用户重定向到某些特殊条件的其他位置。

#3


1  

I would leave it as the generic ActionResult. If you make it the specific result and change it later some, if not all, of your unit tests will need to be rewritten to accommodate the change. This will make your unit tests more brittle than they need to be.

我会把它留作通用的ActionResult。如果您将其作为特定结果并在以后进行更改,则需要重新编写一些(如果不是全部)单元测试以适应更改。这将使您的单元测试比他们需要的更脆弱。

EDIT: additionally, by leaving it as an ActionResult, you allow yourself the ability to return various different results based on you action logic. For example, the normal flow of your method might return a RedirectResult, but you may have error paths that return a ViewResult or HttpUnauthorizedResult. If you type your method more strongly than needed originally, you may end up having to unnecessarily refactor it and your tests as you add in alternative results later.

编辑:另外,通过将其保留为ActionResult,您可以根据您的操作逻辑返回各种不同的结果。例如,方法的正常流可能会返回RedirectResult,但您可能有返回ViewResult或HttpUnauthorizedResult的错误路径。如果您输入的方法比最初需要的方法更强烈,那么您最终可能会在以后添加替代结果时不必要地重构它和您的测试。

The bottom line is that I don't see any real advantages and, at least a couple, disadvantages.

最重要的是,我没有看到任何真正的优势,至少有两个缺点。

#1


6  

I vote yes for two reasons.

我投赞成票有两个原因。

  1. You are explicitly declaring what you expect the method to return and the compiler will catch any attempt to do otherwise. No need for a unit test that does a Assert( result is ViewResult).

    您明确声明了您希望该方法返回的内容,编译器将捕获任何其他尝试。不需要进行Assert的单元测试(结果是ViewResult)。

  2. You have to cast the result to the expected type in your tests when examining any properties unique to that result type (For example, checking Url property of the RedirectResult). Simply declaring the test variable as var removes any brittleness incurred by changing types.

    在检查该结果类型的唯一属性时,必须将结果转换为测试中的预期类型(例如,检查RedirectResult的Url属性)。简单地将测试变量声明为var可以消除因更改类型而导致的任何脆弱性。

#2


2  

By declaring a more specific return type you're gaining a little bit more of compiler type checking that you now don't have to cover in unit tests.

通过声明更具体的返回类型,您可以获得更多的编译器类型检查,您现在不必在单元测试中进行覆盖。

You would be binding yourself, however, to that type, and would have to revert if that changes. A common example is if you have to redirect the user elsewhere on some special conditions by returning a RedirectResult.

但是,您将自己绑定到该类型,并且如果更改则必须还原。一个常见的例子是,如果必须通过返回RedirectResult将用户重定向到某些特殊条件的其他位置。

#3


1  

I would leave it as the generic ActionResult. If you make it the specific result and change it later some, if not all, of your unit tests will need to be rewritten to accommodate the change. This will make your unit tests more brittle than they need to be.

我会把它留作通用的ActionResult。如果您将其作为特定结果并在以后进行更改,则需要重新编写一些(如果不是全部)单元测试以适应更改。这将使您的单元测试比他们需要的更脆弱。

EDIT: additionally, by leaving it as an ActionResult, you allow yourself the ability to return various different results based on you action logic. For example, the normal flow of your method might return a RedirectResult, but you may have error paths that return a ViewResult or HttpUnauthorizedResult. If you type your method more strongly than needed originally, you may end up having to unnecessarily refactor it and your tests as you add in alternative results later.

编辑:另外,通过将其保留为ActionResult,您可以根据您的操作逻辑返回各种不同的结果。例如,方法的正常流可能会返回RedirectResult,但您可能有返回ViewResult或HttpUnauthorizedResult的错误路径。如果您输入的方法比最初需要的方法更强烈,那么您最终可能会在以后添加替代结果时不必要地重构它和您的测试。

The bottom line is that I don't see any real advantages and, at least a couple, disadvantages.

最重要的是,我没有看到任何真正的优势,至少有两个缺点。