Tobias
Tobias

Reputation: 2985

ContentResult vs. string

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";
}

Upvotes: 45

Views: 62954

Answers (6)

christofr
christofr

Reputation: 2700

Two main advantages:

  • You can specify the content encoding via the ContentEncoding property
  • You can specify the content type (ie, 'text/html') via the ContentType property

Also, if you want to be OO-clean about it, ContentResult, along with all ActionResult derived classes follow the Command Pattern by providing an ExecuteResult command for the MVC pipeline to run.

UPDATE: 19-Nov-2020 The original Command Pattern link (dead link) was no longer working so I updated it to reference a similar Microsoft document. Also, to see a great explanation on the Command Pattern, view this link.

Upvotes: 7

Steve Ruble
Steve Ruble

Reputation: 3895

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.

Upvotes: 46

Abhishek Sharma
Abhishek Sharma

Reputation: 11

One another difference is Content result can return different content's result. Like string, Html, JavaScript result etc.

But string return only the string result.

Upvotes: 0

Ziregbe Otee
Ziregbe Otee

Reputation: 554

Returning a ContentResult helps protect your application.

E.g If your application allows input from users, a malicious user might try to parse a javascript text. This will prevent javascript from executing in your application

Upvotes: 1

Amer Jamaeen
Amer Jamaeen

Reputation: 19

One difference as i know is :

Content return result without quotations marks if you want.

String return result in Quotations marks all the time.

Upvotes: 0

Danny Brady
Danny Brady

Reputation: 1935

You can't return a string on a method which returns an ActionResult, so this is when you could use a ContentResult to return a plain string like so:

public ContentResult Hello()
{
    return Content("hello world!");
}

ContentResult by default returns a type of text/plain. If you only ever needed to return a string, then you would use the method to return a string

Upvotes: 17

Related Questions