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.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...