ASP.NET MVC > Output

Output string content from action method in ASP.NET MVC

How to output a string content from controller action method?


To output any content (plain text or HTML) from controller, we mark ContentResult as return type in the action method like below.

CONTROLLER CODE

public ContentResult OutputContent()
        {
            return Content("<strong>This is content.</strong>");
        }

Notice that we are returning string content using the Content function by passing the data as string.

OUTPUT

POINT OF INTEREST

Play with few more overload methods of Content, they help us to pass the ContentType of the data we are returning and also Encoding used.

 Views: 15964 | Post Order: 63



Write for us






Hosting Recommendations