Httpstatuscoderesult & Redirectresult/redirecttorouteresult
In the last installment of this series, we'll discuss the last three Actionresult:httpstatuscoderesult, Redirectresult and Redirecttorouteresult. The first is used to implement a response to an HTTP state, and the latter two are used to implement redirection. As for redirection, it is divided into "temporary redirection" and "permanent redirection", in response state, also known as "302 redirect" and "301 redirect".
First, Httpstatuscoderesult
Each HTTP response has a code that represents the state of the response and an optional status description, which normally returns "OK". System.Web.Mvc.HttpStatusCodeResult makes it easy to return an HTTP response for a specified state. As shown in the following code fragment, Httpstatuscoderesult has statuscode and statusdescription two read-only properties that represent the response status Code and state description information respectively. You can either set the status code to an integer in the constructor or specify the status code in the form of a HttpStatusCode enumeration.
1:public class Httpstatuscoderesult:actionresult
2: {
3: Public httpstatuscoderesult (int statusCode);
4: Public Httpstatuscoderesult (HttpStatusCode statusCode);
5: Public httpstatuscoderesult (int statusCode, string statusdescription);
6: Public httpstatuscoderesult (HttpStatusCode statusCode, string statusdescription);
7:
8: Public override void Executeresult (ControllerContext context);
9:
: public int StatusCode {get;}
One: Public string statusdescription {get;}
12:}
The Httpstatuscoderesult implementation of the request-response logic in the Executeresult method is simple, as shown in the following code snippet, It is only the statuscode and statusdescription that set up the current HttpResponse. It is worth mentioning that if we take the development server of Visual Studio as a host to Web applications, The status description information set by the Statusdescription property of Httpstatuscoderesult does not reflect the HTTP response, and only IIS is used as the host to actually write this information to the response message.
1:public class Httpstatuscoderesult:actionresult
2: {
3: //other Members
4: Public override void Executeresult (ControllerContext context)
5: {
6: Context . HttpContext.Response.StatusCode = this. StatusCode;
7: if (this. Statusdescription!= null)
8: {
9: Context . HttpContext.Response.StatusDescription = this. Statusdescription;
: }
One: }
12:}
The Httpstatuscoderesult has two subclasses, one based on the response state "404, Not Found" System.Web.Mvc.HttpNotFoundResult, and the other is based on the response state "401, not Authorized "System.Web.Mvc.HttpUnauthorizedResult, 7th chapter" Execution of Action " The filter Authorizeattribute returns a Httpunauthorizedresult object if the authorization validation fails.
Second, Redirectresult
Redirectresult helps us achieve a redirect to an address that is exactly the same as the Redirect/redirectpermanent method that invokes Httpresonse. As shown in the following code snippet, Redirectresult has two read-only property permanent and URLs, which represent permanent redirection or temporary redirection, the default value is False, and the latter represents a redirected destination address, which can take an absolute address (for example, http:// www.asp.net), you can also use relative addresses (such as ~/account/register).
1:public class Redirectresult:actionresult
2: {
3: Public redirectresult (string url);
4: Public redirectresult (string URL, bool permanent);
5: Public override void Executeresult (ControllerContext context);
6:
7: Public bool permanent {get;}
8: Public string Url {get;}
9:}
Temporary redirects and permanent redirects can be implemented individually by invoking the redirect and Redirectpermanent of HttpResponse, which in fact redirectresult based on redirection is done by calling both methods. This can be seen by the definition of the Executeresult method as shown below.
1:public class Redirectresult:actionresult
2: {
3: //other Members
4: Public override void Executeresult (ControllerContext context)
5: {
6: //Other operations
7: String url = Urlhelper.generatecontenturl (this. URL, context. HttpContext);
8: if (this. Permanent)
9: {
bool endresponse = false;
One: Context . HttpContext.Response.RedirectPermanent (URL, false);
: }
: Else
: {
: bool Flag2 = false;
: Context . HttpContext.Response.Redirect (URL, false);
: }
: }
19:}