Content Result in ASP.NET Core MVC

Content Result in ASP.NET Core MVC:

In this article, I will discuss the Content Result in ASP in ASP.NET Core MVC Application with Examples. Please read our previous article discussing the JSON Result in ASP.NET Core MVC Application.

Content Result in ASP.NET Core MVC:

In ASP.NET Core MVC, a ContentResult is an action result representing a user-defined content type to be returned to the client. Unlike JsonResult, which returns JSON data, or ViewResult and PartialViewResult, which returns HTML markup, a ContentResult allows you to return a string of content, such as plain text, XML, or any other type of content, directly to the client without needing to render a view. While returning the data from the action method, all we need to specify the content and MIME type.

If you go to the definition of ContentResult, you will see the following signature. This class has a few properties and overrides the ExecuteResultAsync method.

Content Result in ASP in ASP.NET Core MVC Application with Examples

How Does the ExecuteResultAsync Method of ContentResult class Work in ASP.NET Core MVC?

The ExecuteResultAsync method in the ContentResult class is responsible for executing the result of an action method that returns custom content. Here is how the ExecuteResultAsync method works within the ContentResult class:

  • Setting Content and Encoding: Before ExecuteResultAsync is called, the content and, optionally, the content type and encoding of the response are typically set within the Content property of the ContentResult object.
  • Execution: When the action method returns a ContentResult object, the ASP.NET Core MVC framework calls the ExecuteResultAsync method of that ContentResult object.
  • Output: Within the ExecuteResultAsync method, the content set in the Content property is written to the response body, along with any configured content type and encoding.
What are MIME Types?

In ASP.NET Core MVC, MIME types (Multipurpose Internet Mail Extensions) indicate the type of content the server serves. They are essential for correctly interpreting and displaying content in web browsers. 

MIME types enable browsers to recognize the file type of a file sent via HTTP by the webserver. As a result, the browser can choose a suitable displaying method. Common MIME types are, for example, text/html for HTML files or image/jpeg for JPEG files.

How Do We Use ContentResult in ASP.NET Core MVC?

In ASP.NET Core MVC, you can use the ContentResult class to return raw content, such as text, HTML, XML, or JSON, directly to the client without involving view rendering. Start by creating a controller action that will return the raw content. For example, let’s say you want to return a simple plain text message. When returning Plain text, specify the MIME type as “text/plain,” as shown in the example below.

using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            string plainText = "This is plain text content.";

            return new ContentResult
            {
                ContentType = "text/plain",
                Content = plainText
            };
        }
    }
}
Using ContentResult Helper Method:

Alternatively, you can use the Content helper method provided by the Controller class to create a ContentResult. This method simplifies the creation of content results. When returning Plain text, specify the MIME type as “text/plain,” as shown in the example below.

using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            string plainText = "This is plain text content.";
            return Content(plainText, "text/plain");
        }
    }
}
Returning HTML:

When returning HTML content, specify the MIME type as “text/html,” as shown in the example below.

using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            string htmlContent = "<html><body><h1>Hello, Welcome to Dot Net Tutorials</h1></body></html>";
            return Content(htmlContent, "text/html");
        }
    }
}
Returning XML:

When returning XML data, specify the MIME type as “application/xml” as shown in the example below.

using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            string xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><item>Hello Dot Net Tutorials</item></data>";
            return Content(xmlContent, "application/xml");
        }
    }
}
Returning JSON:

When returning JSON data, specify the MIME type as “application/json” as shown in the example below.

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            var jsonData = new { Name = "Pranaya", Age = 35, Occupation = "Manager" };
            return Content(JsonConvert.SerializeObject(jsonData), "application/json");
        }
    }
}
Using String Directly:

The default MIME type is “text/plain”. You can also directly return a string without specifying a content type. In this case, the default content type will be used, which is text/plain, as shown in the example below.

using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            string content = "This is a simple string.";
            return Content(content);
        }
    }
}
Customizing Content:

You can customize various properties of the ContentResult, such as the content type, status code, and more, according to your specific use case.

using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ContentResult Index()
        {
            string customContent = "Custom content with specific settings.";
            return new ContentResult
            {
                Content = customContent,
                ContentType = "text/plain",
                StatusCode = 200, // OK status code
            };
        }
    }
}
Use Cases of Content Result in ASP.NET Core MVC:

ContentResult in ASP.NET Core MVC is used to return raw content, such as text, HTML, XML, or JSON, directly to the client without view rendering. Here are some use cases for ContentResult in ASP.NET Core MVC:

  1. Static Text Responses: You can use ContentResult to return plain text responses to the client. This is useful for displaying messages, instructions, or simple notifications.
  2. Static HTML Pages: You can return static HTML pages as raw content using ContentResult. This can be useful for displaying simple information pages or promotional content.
  3. XML Responses: If your application provides data in XML format, you can use ContentResult to return XML content, which can be useful when integrating with systems that require XML data exchange.
  4. JSON Responses: Like XML, you can use ContentResult to return JSON content. While JsonResult might be a better choice for most JSON use cases, ContentResult can still be employed when you need fine-grained control over the JSON serialization process.
  5. Raw JavaScript or CSS: You can return raw JavaScript or CSS code using ContentResult. This can be useful for injecting dynamic scripts or styles directly into the client’s webpage.

In the next article, I will discuss the FileResult in ASP.NET Core MVC Applications with Examples. In this article, I will explain the ContentResult in ASP.NET Core MVC Applications with Examples. I hope you enjoy this Content Result in an ASP.NET Core MVC Application article.

Leave a Reply

Your email address will not be published. Required fields are marked *