In this tutorial, we will take a look at Action Results in ASP.NET Core like what are Action results? What are the different types of the Action Results that are available? And also we will look at how to create a formatted response from the action method.
Previous tutorials of ASP.NET Core Series
- Introduction to ASP.NET Core 2.0
- How to create a new project in ASP.NET Core 2.0 using Visual Studio.
- Project Folder structure and responsibility of each file in ASP.NET Core 2.0 Application.
- Middleware in ASP.NET Core.
- Routing in ASP.NET Core.
- Attribute-based Routing in ASP.NET Core.
- Tag Helpers in ASP.NET Core 2.0
What are Action Results in ASP.NET Core 2.0
In the controller, Action Methods are responsible to return the results to the client. The result can be in the form of a string, file, complex data like JSON formatted, and the views and etc. As you know controllers are the simple C# classes and these classes do not drive from a base class. The ASP.NET Core provides a controller base class, which is driven from ControllerBase class. This ControllerBase class is provided by Microsoft.AspNet.Mvc. So, the base class gives us access to lots of helper methods about the request, which help us to build results to send back to the client. E.g. RedirectResult will redirect to another action or view, you can send back strings, you can send back integers, you can send back objects, you can send back complex data like JSON formatted, and ViewResult will return the View page, and many things more.
Difference between ActionResult and IActionResult.
ActionResults: ActionResult is a base class for all the action results.
IActionResults: It is an Interface. That defines the contract that represents the result of an action method.
How many types of Action Results are available?
So, there are so many types of Action results are available. And now check out the below list of action results and their behavior.
- ViewResult: It will return the view page.
- PartialViewResult: It will return the partial view page.
- FileResult: It will return files.
- ContentResult: It will return a string.
- EmptyResult: It will return nothing.
- JsonResult: It will return JSON formatted data.
- RedirectToResult: It will redirect to the specific URL.
- RedirectToRouteResult: It will redirect to different action method or different controller action method.
- HttpNotFoundResult: It will return 404 error not found page.
- HttpUnauthorizedResult: It will return 403 HTTP status code.
- FileStreamResult: It will return file content.
- FilePathResult: It will return file content.
- FileContentResult: It will return file content.
How to use Action Results in ASP.NET Core Application?
Let’s take a simple example.
Open the project that we have created in the previous tutorial. Then go to solution explorer, then open the home controller class from under the Controller folder. Then you will see there are three action methods. Now, you just need to copy this below code and replace with the index method from home controller.
As you know ActionResult is an abstract class which implements the IActionResult. And this class is defined in the namespace Microsoft.AspNetCore.Mvc. And in the above index method, as you see the Index method is returning the ContentResult which is one of the Action Result that returns the string.
In this above index method, we have passed a string into the content method, and then this content method produces the ContentResult. This means the index method will now return ContentResult
So, you can also use the ActionResult as a return type. Returning the ActionResult instead of the actual type, will help us to use any of the action result. Look at eh below code.
So, the above index action method will return two ActionResults ContentResult and NotFoundResult depending on the condition.
You can use any kind of Action Result according to your need. Here are below some example.
so, these above examples are enough to understand.
So, in this tutorial, we have learned the concept of Action Results like what is Action Result and what type of action results are available in ASP.NET Core