Common ActionResult types in ASP. net mvc, mvcactionresult

Source: Internet
Author: User

Common ActionResult types in ASP. net mvc, mvcactionresult
I. Definition

In MVC, ActionResult is the return result of Action. ActionResult has multiple Derived classes, and each subclass has different functions. Not all subclasses need to return the View, some directly return the stream, and some return strings. ActionResult is an abstract class that defines the unique ExecuteResult method. The parameter is ControllerContext. The following describes how to use ActionResult in MVC.

2. What is ActionResult?

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.

3. Common ActionResult 1 and ViewResult

A view result, which generates response Content Based on The View template. The corresponding Controller method is View.

2. PartialViewResult

It 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. It corresponds to the PartialView of the Controller method.

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 jump. MVC will generate a Url address based on the specified route name or route information (RouteValueDictionary) and then call Response. Redirect to jump. The corresponding Controller methods are RedirectToAction and RedirectToRoute.

5. 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.

6. EmptyResult

Return an empty result. If the Controller method returns a null result, MVC converts it to an EmptyResult object.

7. 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 executes the returned response content directly. The Controller method corresponding to this result type is JavaScript.

8. JsonResult

Represents a Json result. MVC sets Response. ContentType to application/json, and uses the JavaScriptSerializer class to specify that the object is serialized as a Json representation. Note that by default, Mvc does not allow GET requests to return Json results. To remove this restriction, set the JsonRequestBehavior attribute to JsonRequestBehavior when the JsonResult object is generated. allowGet, which corresponds to the Json of the Controller method.

9. FileResult (FilePathResult, FileContentResult, and FileStreamResult)

These three classes inherit from FileResult, which indicates the content of a file. The difference is that FilePath transfers the file to the client through a path. FileContent uses binary data, while FileStream uses Stream (Stream). Controller provides a File overload method for these three File Result types.

FilePathResult: send a file directly to the client.

FileContentResult: returns byte bytes to the client (comparison)

FileStreamResult: returned stream

10. 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.

11. HttpStatusCodeResult

Returns a server error message.

12. HttpNoFoundResult

An error message indicating that Action cannot be found is returned.

Iv. Relationship table between ActionResult subclass

5. Simple Application of ActionResult (12 types)

Source code:

1 using StudyMVC4.Models; 2 using System; 3 using System. collections. generic; 4 using System. IO; 5 using System. linq; 6 using System. net; 7 using System. net. http; 8 using System. web. http; 9 using System. web. mvc; 10 11 namespace StudyMVC4.Controllers 12 {13 public class HomeController: Controller 14 {15 16 public ActionResult Index () {17 return View (); 18} 19 20 // <summary> 21 // ContentResult usage (returned text) 22 /// http://localhost:30735/home/ContentResultDemo 23 /// </summary> 24 // <returns> return text </returns> 25 public ActionResult ContentResultDemo () {26 string str = "ContentResultDemo! "; 27 return Content (str); 28} 29 30 // <summary> 31 // EmptyResult usage (return null object) 32 /// http://localhost:30735/home/EmptyResultDemo 33 // </summary> 34 // <returns> returns an empty object </returns> 35 public ActionResult EmptyResultDemo () {36 return new EmptyResult (); 37} 38 39 // <summary> 40 // FileContentResult usage (returned image) 41 /// http://localhost:30735/home/FileContentResultDemo 42 // </summary> 43 // <returns> display the content of a file </returns> 44 public ActionResult FileContentResultDemo () {45 FileStream fs = new FileStream (Server. mapPath (@ "/Images/001.jpg"), FileMode. open, FileAccess. read); 46 byte [] buffer = new byte [Convert. toInt32 (fs. length)]; 47 fs. read (buffer, 0, Convert. toInt32 (fs. length); 48 string contentType = "image/jpeg"; 49 return File (buffer, contentType ); 50} 51 52 // <summary> 53 // FilePathResult usage (returned image) 54 /// http://localhost:30735/home/FilePathResultDemo/002 55 /// </summary> 56 /// <param name = "id"> image id </param> 57 /// <returns> directly returns a file object </param> /returns> 58 public FilePathResult FilePathResultDemo (string id) 59 {60 string path = Server. mapPath (@ "/Images/" + id + ". jpg "); 61 // define the content type (image) 62 string contentType =" image/jpeg "; 63 // FilePathResult directly returns the file object 64 return File (path, contentType ); 65} 66 67 // <summary> 68 // FileStreamResult usage (returned image) 69 /// http://localhost:30735/home/FileStreamResultDemo 70 // </summary> 71 // <returns> return file stream (image) </returns> 72 public ActionResult FileStreamResultDemo () 73 {74 FileStream fs = new FileStream (Server. mapPath (@ "/Images/001.jpg"), FileMode. open, FileAccess. read); 75 string contentType = "image/jpeg"; 76 return File (fs, contentType ); 77} 78 79 // <summary> 80 // HttpUnauthorizedResult usage (throwing a 401 error) 81 /// http://localhost:30735/home/HttpUnauthorizedResult 82 // </summary> 83 // <returns> </returns> 84 public ActionResult HttpUnauthorizedResultDemo () 85 {86 return new HttpUnauthorizedResult (); 87} 88 89 // <summary> 90 // method of HttpStatusCodeResult (error status information returned) 91 /// http://localhost:30735/home/HttpStatusCodeResult 92 // </summary> 93 // <returns> </returns> 94 public ActionResult HttpStatusCodeResultDemo () {95 return new HttpStatusCodeResult (500, "System Error "); 96} 97 98 // <summary> 99 // use HttpNotFoundResult 100 /// http://localhost:30735/home/HttpNotFoundResultDemo101 /// </Summary> 102 // <returns> </returns> 103 public ActionResult HttpNotFoundResultDemo () {104 return new HttpNotFoundResult ("not found action "); 105} 106 107 // <summary> 108 // use of JavaScriptResult (return script file) 109 /// http://localhost:30735/home/JavaScriptResultDemo110 /// </Summary> 111 // <returns> return script content </returns> 112 public ActionResult JavaScriptResultDemo () 113 {114 return JavaScript (@ "<script> alert ('test JavaScriptResultDemo! ') </Script> "); 115} 116 117 // <summary> 118 // JsonResult usage (returns a json object) 119 /// http://localhost:30735/home/JsonResultDemo120 /// </Summary> 121 // <returns> returns a json object </returns> 122 public ActionResult JsonResultDemo () 123 {124 var tempObj = new {Controller = "HomeController", Action = "JsonResultDemo"}; 125 return Json (tempObj ); 126} 127 128 // <summary> 129 // usage of RedirectResult (jump url) 130 /// http://localhost:30735/home/RedirectResultDemo131 /// </Summary> 132 // <returns> </returns> 133 public ActionResult RedirectResultDemo () 134 {135 return Redirect (@" http://wwww.baidu.com "); 136} 137 138 // <summary> 139 // RedirectToRouteResult usage (jump action name) 140 /// http://localhost:30735/home/RedirectToRouteResultDemo141 /// </Summary> 142 // <returns> </returns> 143 public ActionResult RedirectToRouteResultDemo () 144 {145 return RedirectToAction (@ "FileStreamResultDemo "); 146} 147 148 // <summary> 149 // usage of PartialViewResult (back to some views) 150 /// http://localhost:30735/home/PartialViewResultDemo151 /// </Summary> 152 // <returns> </returns> 153 public PartialViewResult PartialViewResultDemo () 154 {155 return PartialView (); 156} 157 158 // <summary> 159 // ViewResult usage (return view) 160 /// http://localhost:30735/home/ViewResultDemo161 /// </Summary> 162 // <returns> </returns> 163 public ActionResult ViewResultDemo () 164 {165 // if the View name is not input, by default, View pages with the same name as Action are found. 166 return View (); 167} 168} 169}

 

 

Article Reprinted from: https://www.cnblogs.com/xielong/p/5940535.html

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.