I recently was asked why to use ContentResult
instead of returning string
. Unfortunately I could not give a better answer than: "It is best practice."
Does anyone have a better answer?
To better understand the question. What's the difference?
public ActionResult Foo(){
return Content("Some string");
}
public string Bar(){
return "Some string";
}
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
:
ToString()
call. This doesn't matter if you're returning string
, but returning a complex type could have unexpected results.