Original: ASP.NET Core Action Result - ASP.NET Core Basics Tutorial - simple tutorial, simple programming
ASP.NET Core action results
Previous chapters, we have been using simple C#
class as a controller. Although these classes are not derived from the base class, but you can still use this method in the MVC. Of course, for the controller, but a more common practice from Microsoft.AspNetCore.Mvc
the controller base class namespace derives controller. In this chapter, we will try to do so, and learning operation result (Action Results).
The results of actions (Action Result)
Microsoft.AspNetCore.Mvc
Under the base class namespace Controller
allows us to access a lot of information about the context of HTTP requests, and provides some methods to help build our return to the results back to the client
The results returned in response, we can send a simple string or integer, or send an object like this to represent complex data such as students or university or restaurant information, and associated with the object's data
These results are typically packaged to achieve IActionResult
the object of the interface, there are a number of different types of implements this interface results, these results may include content type or model for file downloads
These different types of results which can return JSON, can return XML, or HTML view
Return operation can be substantially any type of different operation result. They have a common base class:ActionResult
The following table lists the results of different kinds of actions and their behavior
Action name (class) | behavior |
---|---|
ContentResult | Returns the string of a string |
FileContentResult | Returns the contents of the file |
FilePathResult | The return path of the file contents |
FileStreamResult | Returns the content stream files |
EmptyResult | Return empty |
JavaScriptResult | Return some JavaScript code |
JsonResult | Return data JSON format |
RedirectToResult | Redirect to a URL |
HttpUnauthorizedResult | Returns the status code 403 Unauthorized |
RedirectToRouteResult | Redirect to a different controller or a method |
ViewResult | It returns a response to the view from the engine |
PartialViewResult | It returns a response to the view from the engine |
Examples: ContentResult
Now, we modify HomeController.cs
the introduction of the namespace Microsoft.AspNetCore.Mvc
, and modify HomeController
inherited from Controller
.
The following code is the HomeController
full implementation of the class
a using System ;
a using Microsoft.AspNetCore.Mvc ; namespace HelloWorld.Controllers { public class the HomeController : the Controller { public ContentResult Index () { return Content ( "Hello, world news from the use of this Action Result of Home Controller!" ) ; } } }
We can see that the Index()
method returns a ContentResult
result type. ContentResult
It is to achieve ActionResult
one of the results of different types of interfaces
In the Index()
process, we will pass to a string Content()
. Content()
The method produces a ContentResult
, that is, Index()
the method returns ContentResult
Save HomeController.cs
the file, restart the application, and then access the root directory /
, we will get the following output
We can see that the response and the response before we see almost no difference, it's still just a plain text response
You may be wondering what use to generate ActionResult What are the advantages
In Mvc mode, the controller decides what to do next, returns a string or HTML or return the model objects may be serialized into JSON, etc.
Mvc the controller needs to do is to make a decision, but the controller does not have to be written decision results directly in the response. It only needs to return a result, the framework will then use these results and understand how to convert the result to be sent back via HTTP content
Examples: ObjectResult
If you can not understand the contents of the above, it does not matter, let's look at an example, this time we use ObjectResult
In Solution Manager HelloWorld
, right-click on, create a new folder and name it Models
. In the Models
folder, add a representation for employees in Employee
class
Once created, the directory structure is as follows
Employee.cs
The contents are as follows
using System;
namespace HelloWorld.Models
{ public class Employee { public Employee() { } } }
Modifying just created Employee
class, the two attributes, an integer ID
and a string type of Name
modification is completed after Employee.cs
the contents are as follows
using System;
namespace HelloWorld.Models
{ public class Employee { public Employee() { } public int ID { get; set; } public string Name { get; set; } } }
Then we go back to HomeController
the controller, modify the Index()
method that returns an Employee
object.
After the modification is completed HomeController.cs
as follows
using System;
using Microsoft.AspNetCore.Mvc; using HelloWorld.Models; namespace HelloWorld.Controllers { public class HomeController: Controller { public ObjectResult Index() { var employee = new Employee { ID = 1, Name = "语飞"}; return new ObjectResult(employee); } } }
Now, the return is not Content, but returns the result of a different type ObjectResult
. If we want a ObjectResult, we need to create or instantiate a ObjectResult and some of the model object as a parameter passed to it
In the MVC framework, ObjectResult is special because when we return to a ObjectResult, MVC framework will access the object. And converting the object to do something, and then as an HTTP response back to the client
When converting ObjectResult object, it may be serialized as XML or JSON or another format, as to what format, when you start the configuration information provided by the application to the MVC decision. If we do not explicitly configure anything, it will be used as the default format JSON
Save all files, restart the application, and then visit the home page, we will get the following results