ActionResult of ASP. NET MVC

Source: Internet
Author: User
Tags blank page

When the action is finished, the value of the postback is manipulated by the ActionResult category or its derived categories. ActionResult is an abstract class, so ASP. NET MVC itself is a subclass of many different types of actionresult.

ActionResult sub-class and Description:

Common Viewresult used to pass a view, that is, HTML page content;

Partialviewresult a view, but this view is Partialview;

Redirectresult is used to turn Web pages to other URLs;

Emptyresult is used to return a blank page;

Contentresult returns a literal attribute (text content);

Fileresult returns a binary document;

Filecontentresult returns a binary file that can be downloaded;

Filepathresult returns a binary file that can be downloaded and has a path set;

Filestreamresult returns a stream file that can be downloaded;

Jsonresult returns a JSON result;

Javascriptresult returns a JavaScript object.

These are categories that inherit from ActionResult and can also be used as the type of action.

But the return type we often define in the controller is ActionResult, but the returned value is often something else, such as:

1 // 2  // GET:/product/ 3   Public actionresult Index () 4 {5       return View (); 6  }


This is to think that the object returned by the view is Viewresult.

Controller Common method redirect returns the object is Redirectresult;

The object returned by Redirecttoaction is Redirecttoactionresult;

The object returned by Redirecttoroute is Redirecttorouteresult;

The object returned by JSON is Jsonresult;

The object returned by Javascriptresult is Javascriptresult;

Content returns the object is Contentresult;

The object returned by file is Filecontentresult, Filepathresult, Filestreamresult , and so on;

Here are a few examples

Back to Partialview

1         Publicactionresult productlist ()2         {3PRODUCTBLL PRODUCTBLL =NewPRODUCTBLL (httpcontext.application["efconnectionstring"]. ToString ());4 5ienumerable<product> products =productbll.listproducts ();6 7             returnPartialview ("~/views/instock/_products.cshtml", products);8}


Return JSON

1          PublicActionResult Retrieveproduct (intID)2         {3PRODUCTBLL PRODUCTBLL =NewPRODUCTBLL (httpcontext.application["efconnectionstring"]. ToString ());4 5Product Product =productbll.retrieveproduct (ID);6 7             returnJson (product,jsonrequestbehavior.allowget);8}


Back to PDF

1          Publicactionresult loadpdffile ()2         {3             stringPath =@"C:\ZJF\My Team solutions\allure\web\backofficev2\google_merchant_center_ Quick Operation manual. pdf";4FileStream stream =NewFileStream (Path, FileMode.OpenOrCreate, fileaccess.readwrite);5 6             returnFile (Stream,"application/pdf");7}

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.