Menu

Tuesday, 28 October 2014

ContentResult in MVC

About

ContentResult is one of the ActionResult type in MVC. This is mostly used to display dynamic content to the user based on various conditions. So, you can return various types of content like plain text, xml, html etc.

In this article you are going to learn about the following things.
  1. ContentResult and its properties
    • Content
    • ContentType
    • ContentEncoding
  1. Return various Types of Content
    • Plain Text
    • Html
    • XML
ContentResult and its Properties

Content: Its used to send the content data in a string format.

ContentType: It helps to indicate the content type. The default content type for ContentResult is text/html; charset=utf-8.

ContentEncoding: It helps to indicate the content encoding type, the default encoding  type  for ContentResult  is UTF-8.
Return Various Types of Content

In this scenario, you will get a more idea by creating sample application with the following these steps.

Step 1:
Create a new project with the name ContentResultDemo and choose the template as Empty as shown in the following screen shots.



Now, click on Ok button then the screen would display like the below.



Step 2:
After clicking on Ok button the project would be created.  Then create a controller named as “ContentDemoController” like the below.


Now, click on "Controller" then the displayed screen is as follows.



Click on "Add"  button then the output template window is as follows.



Enter the name as "ContentDemoController" as mentioned in the aforesaid screenshot and then click on "Add" button then controller would be created and its displayed like the below.



Step 3:
Now, Replace the default code in the ContentDemoController.cs file with the following code.

using System.Web.Mvc;

namespace ContentResultDemo.Controllers
{
    public class ContentDemoController : Controller
    {
        // GET: ContentDemo
        public ActionResult Index()
        {
            return Content("test content");
        }
    }
}

And then Build the application (hit F6) and run the application (hit F5) then navigate to the following Url (http://localhost:50589/ContentDemo/)

Note: The port address in the above Url might be a chance to changed at your end.

The output screen looks like below.



Till now, you have created one Index action method with sample test content in the ContentDemoController.cs  file.

Step 4:

Add the following code in ContentDemo controller class file which helps to return sample content based on its content type.

        /// 
        /// Returns sample content based on its content type
        /// 
        /// content type
        /// As String
        public string SampleContent(int contentType)
        {
            switch (contentType)
            {
                case (int)ContentTypes.PlainText:
                    return "Plain Text";
                case (int)ContentTypes.Html:
                    return "
Name
Ramchand
Test
"; case (int)ContentTypes.Xml: return "" + "Ramchand" + "Srinivas" + ""; default: return string.Empty; } } /// /// Content Types enum /// public enum ContentTypes { PlainText = 1, Html, Xml }


Plain Text:

To display the plain text by using ContentResult as return type, you have to add the below action method.


        /// 
        /// Returns Plain Content
        /// 
        /// 
        public ActionResult PlainContent()
        {
            return Content(SampleContent((int)ContentTypes.PlainText), 
                        "text/plain");
        }

Now, run the application (hit F5) and navigate to this Url (http://localhost:50589/ContentDemo/PlainContent) then the displayed output is like the below.



Html:

To display the Html output, add the following action controller method with this code

        /// 
        /// Returns Html Content
        /// 
        /// 
        public ActionResult HtmlContent()
        {
            return Content(SampleContent((int)ContentTypes.Html), 
                        "text/html");
        }

Now, run the application (hit F5) and navigate to this Url (http://localhost:50589/ContentDemo/HtmlContent) then the displayed output is like the below.



Xml:

To display the Xml output, add the below action method in the ContenDemoController.cs file

        /// 
        /// Returns Xml Content
        /// 
        /// 
        public ActionResult XmlContent()
        {
            return Content(SampleContent((int)ContentTypes.Xml), 
                        "application/xml");
        }

Now, run the application (hit F5) and navigate to this Url (http://localhost:50589/ContentDemo/XmlContent) then the displayed output is like the below.



As, you have observed in the above examples, the Content property for Content Result is only string. Even though while displaying at browser it would changes based on the content type which you have specified in the Content Result.

The whole code you have implemented in the ContentDemoController.cs file is as follows.

using System.Web.Mvc;

namespace ContentResultDemo.Controllers
{
    public class ContentDemoController : Controller
    {
        // GET: ContentDemo
        public ActionResult Index()
        {
            return Content("test content");
        }

        /// 
        /// Returns Plain Content
        /// 
        /// 
        public ActionResult PlainContent()
        {
            return Content(SampleContent((int)ContentTypes.PlainText), 
                        "text/plain");
        }

        /// 
        /// Returns Html Content
        /// 
        /// 
        public ActionResult HtmlContent()
        {
            return Content(SampleContent((int)ContentTypes.Html),
                        "text/html");
        }

        /// 
        /// Returns Xml Content
        /// 
        /// 
        public ActionResult XmlContent()
        {
            return Content(SampleContent((int)ContentTypes.Xml), 
                        "application/xml");
        }

        /// 
        /// Returns sample content based on its content type
        /// 
        /// content type
        /// As String
        public string SampleContent(int contentType)
        {
            switch (contentType)
            {
                case (int)ContentTypes.PlainText:
                    return "Plain Text";
                case (int)ContentTypes.Html:
                    return "
Name
Ramchand
Test
"; case (int)ContentTypes.Xml: return "" + "Ramchand" + "Srinivas" + ""; default: return string.Empty; } } /// /// Content Types enum /// public enum ContentTypes { PlainText = 1, Html, Xml } } }

Conclusion
 
I hope you got an idea about ContentResult and its properties and how can you send various types of content by just changing the ContentType property in ContentResult. Please provide your suggestions and comments if any.

No comments:

Post a Comment