Understanding the nature of several actionresult of asp.net mvc: Emptyresult & Contentresult

Source: Internet
Author: User
Tags abstract execution httpcontext implement

Most of the action methods defined in controller return a ActionResult object. ActionResult is an encapsulation of the action execution result that ultimately responds to the request. asp.net MVC provides a series of actionresult, in what way do they respond to requests in essence? This is the focus of this series on topics.

ActionResult response to a request

HTTP is a simple network protocol that uses the request/Reply message exchange Mode, and the Web server responds to requests based on processing results after receiving and processing requests from the client. For access requests from the client, the final processing is reflected in the execution of the target action method, and we can artificially control the response to the request when defining the action method. If the following code fragment shows that the abstract class controller has a read-only response attribute that represents the current HttpResponse, we can use it directly to implement the response to the request. We can also indirectly obtain the HttpResponse object used to respond to the request by representing the HttpContext property of the current HTTP context and the ControllerContext property representing the controller context.

   1:public abstract class Controller:controllerbase, ...
2: {
3: //other Members
4: Public httpresponsebase Response {get;}
5: Public httpcontextbase HttpContext {get;}
6:}
7:
8:public Abstract class Controllerbase:icontroller
9: {
All: //other Members
One: public controllercontext ControllerContext {get; set;}
12:}

In principle, we can use HttpResponse to control the request response by 100%, but we do not generally do so, but instead implement the response to the request in a ActionResult object. As the following code fragment shows, ActionResult is an abstract type, and the final request response is implemented in an abstract method Executeresult method.

   1:public Abstract class ActionResult
2: {
3: //other Members
4: Public abstract void Executeresult (ControllerContext context);
5:}

As the name suggests, ActionResult is the result of executing the action. Actioninvoker after the execution of the action method is completed, if a ActionResult object is returned, Actioninvoker invokes the current controller context as a parameter to its Executeresult method. The final rendering of view is done through ActionResult subclass Viewresult, except viewresult,asp.net MVC also defines additional specific actionresult for us.

Second, Emptyresult

We talked about the ActionResult object returned by the action method that was called Actioninvoker to implement the response to the current request, which is not accurate enough. Regardless of whether the action method has a return value, or whatever its return value is, Actioninvoker eventually creates the corresponding ActionResult object. If the action method returns a type of void, or if the return value is NULL, the end result is a Emptyresult object.

As shown in the following code snippet, Emptyresult actually did nothing in the overridden Executeresult method, so Emptyresult is an "empty" actionresult. The design of Emptyresult embodies a design idea: we use a piping design to complete the processing of a request, such as ASP.net mvc is "the execution of the action method =〉 based on the execution result actionresult=〉 Execute ActionResult ", but this process is not suitable for some special requests (such as the action method does not have a return value or the return value is NULL, then the following two links can be ignored), We do some matching work on these exceptional scenarios so that we can handle all the requests in a uniform way, so Emptyresult has an adaptor here.

   1:public class Emptyresult:actionresult
2: {
3: Public override void Executeresult (ControllerContext context)
4: {
5: }
6:}

Third, Contentresult

Contentresult enables ASP.net MVC to respond to requests according to the content we specify. As shown in the following code snippet, we can use the Contentresult content property to specify the contents of the response as a string. The other two properties ContentEncoding and contenttype are used to specify the character encoding and media type (MIME type). The abstract class controller defines the following three protected content method overloads to create corresponding Contentresult based on the specified content, encoding, and media type.

   1:public class Contentresult:actionresult
2: {
3: Public override void Executeresult (ControllerContext context);
4:
5: Public string Content {get; set;}
6: Public Encoding contentencoding {get; set;}
7: Public string ContentType {get; set;}
8:}
9:
10:public abstract class Controller:controllerbase, ...
11: {
A: //other Members
protected Contentresult Content (string content);
protected Contentresult Content (string content, string contentType);
: protected Virtual Contentresult content (string content, String contentType, Encoding contentencoding);
16:}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.