Content Result in ASP.NET MVC

Content Result in ASP.NET MVC

In this article, I am going to discuss Content Result in the ASP.NET MVC application. Please read our previous article where we discussed FileResult in ASP.NET MVC. ASP.NET MVC has different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. Action Results return the result to the view page for the given request.

Content Result in ASP.NET MVC:

The Content Result in ASP.NET MVC returns different content formats to the view like HTML format, JavaScript format, or any other format. We need to use the ContentResult in the ASP.NET MVC Application when we want to allow the action to specify what should it returned. All we need to do is specify the content and MIME type.

Example to Understand Content Result in MVC:

Let us understand how to use Content Result in ASP.NET MVC Application with an example. Please modify the HomeController as shown below. Here, we are using ContentResult as the return type of the Index action method and also we are returning HTML content.

public class HomeController : Controller
{
    public ContentResult Index()
    {
        return Content("<h3>Here's a custom content header</h3>", "text/html", System.Text.Encoding.UTF8);
    }
}

Calling this action will display the h3 tag in the browser as shown in the below image.

Content Results in ASP.NET MVC

There are three different overloaded versions available for ContentResult in ASP.NET MVC Framework as shown in the below image.

Content Results Overloaded Versions in ASP.NET MVC

Parameters:
  1. content: The content to write to the response.
  2. contentType: The content type (MIME type).
  3. contentEncoding: The content-encoding.
ContentResult vs String:

An interesting fact about ContentResult is that if you do this:

public class HomeController : Controller
{
    public string Index()
    {
        return "<h3>Here's a custom content header</h3>";
    }
}

ASP.NET MVC Framework actually creates a ContentResult and wraps it around the returned value and it doesn’t have to be a string. But if we return null, in that case, MVC returns an EmptyResult. When we need to explicitly return ContentResult:

  1. 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.
  2. If in the future you need to specify the content type you won’t need to change the method signature.
  3. It doesn’t hide the ToString() call. This doesn’t matter if you’re returning a string, but returning a complex type could have unexpected results.

In the next article, I am going to discuss EmptyResult in ASP.NET MVC Application. Here, in this article, I try to explain ContentResult in ASP.NET MVC application with examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.