IActionResult

If you noticed, the default return type in the action method of the controller was IActionResult, and then we changed the return type to the string in order to return the text Hello World!.

The IActionResult is the interface that we can use to return different types of ActionResult, ranging from a simple string to complex JSON data, so, we don't need to change the return type of the action method to return the string.

In the earlier example, the return type was changed to the string to make things simple. Now, let us make a simple change to return the string by keeping the return type (IActionResult) as it is:

// GET: /<controller>/
public IActionResult Index()
{
return Content("Hello World! I am learning MVC!");
}

While returning the string, we are using the virtual method called Content from the Controller class (the base controller from where HomeController is inherited from) in the preceding action method. The purpose of this Content() method is to convert the string to the type IActionResult.

IActionResult is capable of returning different data types:


These are actually methods in the ControllerBase class.