If you return something other than an ActionResult
the default behavior is to create a ContentResult
wrapping the result of calling ToString()
on whatever you did return (or EmptyResult
if you returned null
). Reasons I can think of to explicitly return ContentResult
:
- It reinforces the fact that the method is an action, rather than a regular method, so devs are less likely to make mistakes like casually renaming it.
- If in the future you need to specify the content-type you won't need to change the method signature.
- It doesn't hide the
ToString()
call. This doesn't matter if you're returning string
, but returning a complex type could have unexpected results.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…