Extension point in MVC (6) actionresult

Source: Internet
Author: User

Actionresult is the result type returned after the Controller method is executed. The Controller method can return a type that is directly or indirectly inherited from the actionresult abstract class. If the returned type is not actionresult, the Controller converts the result to a contentresult type. The default controlleractioninvoker calls the actionresult. executeresult method to generate a response result.

The default actionresult implemented in MVC is as follows:

1,Contentresult: Return simple plain text content. You can use the contenttype attribute to specify the type of the Response Document and the contentencoding attribute to specify the character encoding of the Response document. You can use the content method in the Controller class to conveniently return the contentresult object. If the Controller method returns a non-actionresult object, MVC will generate a contentresult object based on the tostring () content of the returned object.
2,Emptyresult: Return an empty result. If the Controller method returns a null value, MVC converts it to the emptyresult object.
3,Redirectresult: Indicates a connection jump, which is equivalent to the response. Redirect method in ASP. NET. The corresponding Controller method is redirect.
4,Redirecttorouteresult: It also indicates a redirection. MVC will generate a URL address based on the specified route name or route information (routevaluedictionary) and then call response. Redirect to redirect. The corresponding controller methods are redirecttoaction and redirecttoroute.
5,Viewresult: Indicates a view result, which generates response Content Based on The View template. The corresponding Controller method is view.
6,Partialviewresult: Indicates a part of the view result, which is essentially the same as viewresult, but some views do not support the master. Corresponding to ASP. NET, viewresult is equivalent to a page, while partialviewresult is equivalent to a usercontrol. The corresponding Controller method is partialview.
7,Httpunauthorizedresult: Indicates an unauthorized access error. MVC sends a 401 response status to the client. If form Authentication mode = "forms" is enabled in Web. config, the 401 status redirects the URL to the specified loginurl link.
8,Javascriptresult: It is essentially a text content, but only response. contenttype is set to application/X-Javascript. This result should be consistent with that of microsoftmvcajax. when JS scripts are used together, the client determines the response after receiving the Ajax response. contenttype value. If it is application/X-JavaScript, Eval directly executes the returned response content. The Controller method corresponding to this result type is JavaScript.
9,Jsonresult: Represents a JSON result. MVC sets response. contenttype to application/JSON, and serializes the specified object to a JSON representation through the javascriptserializer class. Note that by default, MVC does not allow GET requests to return JSON results. To remove this restriction, set the jsonrequestbehavior attribute to jsonrequestbehavior. allowget when the jsonresult object is generated. The Controller method corresponding to this result is JSON.
10,Filepathresult, filecontentresult, and filestreamresult: These three classes inherit from fileresult to indicate the content of a file. The difference between the three classes is that filepath transfers the file to the client through a path, and filecontent uses binary data, filestream is transmitted through stream. Controller provides a file overload method for these three file Result types.

You can customize the result type by inheriting the actionresult directly or indirectly. In the following example, an xmlresult type is implemented to return the XML response content:

1. Create an empty MVC Project

2. Implement the xmlresult class

Show row number copy code? Xmlresult
  1. public class XmlResult : ActionResult
  2.  {
  3.     public XmlResult(Object data)
  4.     {
  5.         this.Data = data;
  6.     }
  7.     public Object Data
  8.     {
  9.         get;
  10.         set;
  11.     }
  12.     public override void ExecuteResult(ControllerContext context)
  13.     {
  14.         if (Data == null)
  15.         {
  16.             new EmptyResult().ExecuteResult(context);
  17.             return;
  18.         }
  19.         context.HttpContext.Response.ContentType = "application/xml";
  20.         using (MemoryStream ms = new MemoryStream())
  21.         {
  22.             XmlSerializer xs = new XmlSerializer(Data.GetType());
  23.             xs.Serialize(ms, Data);
  24.             ms.Position = 0;
  25.             using (StreamReader sr = new StreamReader(ms))
  26.             {
  27.                 context.HttpContext.Response.Output.Write(sr.ReadToEnd());
  28.             }
  29.         }
  30.     }
  31. }

3. Create a homecontroller and implement the index method.

public ActionResult Index()
{
    return new XmlResult(new Product()
    {
        ID = "000001",
Name = "Test? ",
        Description = ""
    });
}
 

 

Source code download

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.