MVC Learning Series -- ActionResult extension, mvcactionresult

Source: Internet
Author: User

MVC Learning Series -- ActionResult extension, mvcactionresult

First, MVC is highly scalable.

I started with the extension of ActionResult, because we know that Microsoft ActionResult and its subclass cannot satisfy all returned values.

For example, I need to return XML.

Therefore, now I extend XMLResult to inherit ActionResult.

Step 1: Create an XmlResult

 1 public class XmlResult:ActionResult 2     { 3         private object _data; 4  5         public XmlResult(object data) 6         { 7             this._data = data; 8         } 9         public override void ExecuteResult(ControllerContext context)10         {11             var serializer = new XmlSerializer(_data.GetType());12             var reponse = context.HttpContext.Response;13             reponse.ContentType = "text/xml";14             serializer.Serialize(reponse.Output, _data);15         }16     }

Step 2: Create a StudentViewModel class for demonstration

1 public class StudentViewModel2     {3         public string ID { get; set; }4         public string Name { get; set; }5         public string Gender { get; set; }6     }

Step 3: Add GetXmlResult in HomeController

1  public XmlResult GetXmlResult()2         {3             StudentViewModel viewModel = new StudentViewModel();4             viewModel.ID = "1";5             viewModel.Name ="Zhangsan";6             viewModel.Gender = "Man";7 8             return new XmlResult(viewModel);9         }

Step 4: Test
Enter Home/GetXmlResult in IE

 

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.