• About Me
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Advertise / Sponsor

DOTNET DETAIL

Learn Microsoft .NET Technologies

  • Home
  • Tutorials
    • Angular
      • Angular 5
      • Angular 6
    • ASP.NET Core
    • Azure
    • React
    • Vue
  • Books
  • Courses
  • Cloud Hosting
  • Interview Questions
You are here: Home / ASP.NET Core / Action Results in ASP.NET Core 2.0

Action Results in ASP.NET Core 2.0

May 16, 2018 by mebakar1005 Leave a Comment

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.

See also  How to add NSwag to Asp Net Core 3.0 web API and generate client code using NSwagStudio

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.
  • HttpStatusCodeResult
  • HttpUnauthorizedResult: It will return 403 HTTP status code.
  • JavascriptResult: It will return scrips for execution.
  • 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.

See also  Project Folder Structure of ASP NET Core 2.0 Application

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.

PartialViewResult

ViewResult

FileContentResult

FileStreamResult

so, these above examples are enough to understand.

Conclusion

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

Tag Helpers in ASP.NET Core 2.0
What is Entity Framework Core in ASP.NET Core 2.0

Related

Filed Under: ASP.NET Core Tagged With: .NET CORE, ACTION RESULTS, ACTION RESULTS IN ASP.NET CORE, ASP.NET, ASP.NET CORE, ASP.NET CORE 2.0, ASP.NET MVC6

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Buy me a coffeeBuy me a coffee

Jobs

Dotnet Jobs

Join Us

Join Us

Subscribe to Blog via Email

Enter your email address to subscribe.

Recent Posts

  • Realmente Hace Él En absoluto como yo ?
  • Fiscal Planning — What You Need to Know
  • Organization Strategies for Good Business Success
  • Contemporary Business The usage
  • Deciding on a Document Management Program
  • Most Popular Dating Apps For Black Gay – Remanufactured Online Hookup for Gays
Copyright © 2022