of. NET MVCActionResult
ActionResult is the parent class for all controler return values. All the results are packaged by ActionResult and sent to the client.
Inheritance structure
- System. Object
- System.Web.Mvc.ActionResult Span style= "font-family: ' Segoe UI ', ' Lucida Grande ', Verdana, Arial, Helvetica, Sans-serif; Letter-spacing:normal; line-height:17.5499992370605px; " >
- System.web.mvc . Contentresult
- system.web.mvc Emptyresult
- system.web.mvc Fileresult
- system.web.mvc Httpstatuscoderesult
- system.web.mvc Javascriptresult
- system.web.mvc Jsonresult
- system.web.mvc Redirectresult
- system.web.mvc Redirecttorouteresult
- system.web.mvc Viewresultbase
The
can be divided into four categories from the basic structure. view class , text class , file class , and status code class .
View class returns results
Start with the view class that we are most familiar with. View () and Partialview () They return ViewResult and partialviewresultrespectively. From the first time we approached. NET MVC was in use. The function is to give the processed data to the view engine (if any) to render the view.
| return value |
Help Methods |
Description |
ViewResult
|
View
|
Render a Page view
|
Partialviewresult
|
Partialview
|
Renders a Web page view, but does not use layout pages
|
In general, the view we create for each action is a partial view that contains only a subset of the pages. View () after rendering the view, the results are embedded in the layout page, returning all of the pages, and Partialview () sends the results directly to the client after rendering is finished.
Text class returns results
| return value |
Help Methods |
Description |
Contentresult
|
Content
|
Returns a string literal in which the content format of the text can be specified by MIME
|
Jsonresult
|
Json
|
Serializes the data into JSON, which is then returned to the client |
Javascriptresult
|
Javascript
|
Returns a string in JavaScript format |
Content ()
There is no difference between using the content () method and directly returning a string string, except that you can specify the format of the returned content (XML or HTML) and character encoding .
- Public string Content ()
- {
- return " ; //Browser display Hellokitty
- }
- Public ActionResult Content2()
- {
- //return Content ("
- //Specifies the format and character encoding of the returned text
- return Content ("", "text/html",System. ) Text. Encoding. UTF8);
- }
- The effect is the same.
Json ()
Pass in an object of any type, and format it as JSON as possible. I've tried basically any type of object can be formatted, but for a file or a picture of something like that after formatting is just a lot of useless garbled.
Generally speaking,Dictionary This type of key-value pair will be converted to JS class ,List This class will be converted to JS array .
- class Student
- {
- Public string Name { get; Set ; }
- Public int Age { get; Set ; }
- }
- Public ActionResult JSON1()
- {
- var array = new List<Student> ();
- Array. ADD (new Student { Name = "xiaoming", age = c14>});
- Array. ADD (new Student { Name = "Xiao Li", age =
- return Json (array,jsonrequestbehavior. Allowget); //jsonrequestbehavior is used to specify whether get access is allowed, only post is allowed by default
- //Run Result: [{"Name": "Xiaoming", "Age": 12},{"name": "Xiao Li", "Age": []
- }
- Public ActionResult JSON2()
- {
- //You can also use anonymous inner classes to save data
- return Json (new { name = "Test", age = +, sex = "Boy." }, jsonrequestbehavior. Allowget);
- //Run Result: {"name": "Test", "age": +, "Sex": "Boy"}
- }
JavaScript ()
JavaScript () directly returns a JavaScript code string. The effect of JavaScript () is actually the same as content ( ), except that JavaScript () automatically specifies that the content of the returned text is application/ X-javascript.
- Public ActionResult JS()
- {
- return JavaScript ("alert ('" + DateTime. ) Now. tolongtimestring () + "')");
- }
- Public ActionResult JS2()
- {
- return Content ("alert ('" + DateTime. ) Now. tolongtimestring () + " ')", "Application/x-javascript");
- //This writing effect is exactly the same as above
- }
- /*
- <! DOCTYPE html>
- <meta charset= "UTF-8" >
- <script src= "Http://localhost:5695/home/js2" ></script>
- <!--can fill in the action's address directly in the script tag--
- <body></body>
- */
The file class returns the result
The method that can be used to return files in controler is only file (). File () can send files in a variety of forms, through file names , binary byte[], stream streams. The file type must also be indicated by MIME when sending.
- Public ActionResult FILE1()
- {
- System. IO. Stream fs = System. IO. File. OpenRead (@"test.png");
- return File (fs, @"image/png"); //Flow through the method
- }
- Public ActionResult FILE2()
- {
- return File (@"test.png", @"image/png"); //By the way the file name
- }
- Public ActionResult FILE3()
- {
- System. Drawing. Bitmap b = new System. Drawing. Bitmap (a ); //Create a blank picture
- System. IO. MemoryStream ms = new System. IO. MemoryStream ();
- b. Save (Ms, System. Drawing. Imaging. ImageFormat. Bmp);
- byte [] bytes = ms. GetBuffer ();
- return File (bytes, @"image/bmp"); //way of binary data
- }
- /*
- It is now possible to assign an address directly to an IMG tag for display
- */
Note: The returned file is not too large, otherwise it will cause memory overflow. If you want to download large files, it is best to return the file address directly.
Implement anti-theft chain
If you simply return a file, it is too much of a meaning. Return a file via the controller with respect to the file address directly to the user, we can do some processing, such as only allow logged users to view this image, or to add a watermark on the image.
- Public ActionResult FILE1()
- {
- if (HttpContext. User. Identity. IsAuthenticated)
- //If the user is logged in, the picture will be displayed
- return File (@"test.png", @"image/png");
- Else
- //Do not log in to display a picture of the anti-theft chain
- return File (@"test.png", @"image/png");
- }
Implementing the Verification code
It is returned to the client by dynamically generating a picture. Refer to my post. NET MVC Verification code for specific steps.
Status Code class returns results
| return value |
Help Methods |
Description |
Redirectresult
|
Redirect
|
Returns an HTTP 302 status code that causes the client browser to jump to the specified URL
|
Redirecttorouteresult
|
Redirecttoaction
|
Returns an HTTP 302 status code that causes the client browser to jump to the specified action
|
Redirecttorouteresult
|
Redirecttoroute
|
Jumps to the specified URL, depending on the routing API
|
Httpstatuscoderesult
|
(None)
|
Returns a specific HTTP status code and Description
|
Httpunauthorizedresult
|
(None)
|
Returns an HTTP 401 status code prompting the user not to log in
|
Httpnotfoundresult
|
Httpnotfound
|
Returns an HTTP 404 status code prompting the user to access a resource that does not exist
|
Emptyresult
|
(None)
|
Returns an HTTP 200 status code indicating that the request was successful, but that it does not contain any information
|
If the action Returns a result of null, Controler automatically replaces null with emptyresult.
ActionResult of. NET MVC