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"; } 
                ContentResult represents a user-defined content type. It is inherited from ActionResult. ContentResult has the following three properties: Content that we want to render on browser. ContentEncoding that defines the encoding of the content.
You can't return a string from a method which returns an ActionResult, so in this case you return Content("") as swilliams explained. If you only ever need to return a string, then you would have the method return a string, as Phil explained.
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With