Content Result in Controller Sample in MVC

Content Result in Controller Sample in MVC

Introduction

In this article we will see how to use content results in a controller in MVC.

Step 1

Create a MVC project from the "Empty" template.

Right-click on "Controllers" and select "Add" >> "Controller...".

image.png

Step 2

Select "MVC 5 Controller - Empty" to add an empty controller.

Click on the "Add" button

image.png

Step 3

Name the controller as in the following:

image.png

And it will add an action result method in the controller.

image.png

Step 4

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 ContentType that informs the browser about the content like "text/plain", "text/html", "application/json" and so on.

image.png

Here the ContentResult method returns an object of ContentResult. In the first parameter we pass the content that we want to render on the browser. The second parameter is of ContentType and the third one is content encoding.

image.png

image.png

Step 5

Run the project and you will see, content is rendered in the browser

image.png

<< Day 8

>> Day 10