By learning, we can find that many different action return types are available in the controller.
So what does that really do for them? What are their usages and differences? Through the information book on the introduction and online information query, here to give you a list and general summary.
(1). ActionResult (Base): The most basic type of action, return other types can write ActionResult.
(2). Contentresult: Returns the Contentresult user-defined content type.
Public actionresult Content () { return content ("Test content""text/html") // can specify text type
page Output "Test content"; This type is used for text content that needs to be returned in an AJAX operation.
You can also add such as JavaScript scripts ("Text/javascript") and CSS styles ("Text/css"), and so on, you can modify the style and JS and so on. (3).
emptyresult: returns a null or Emptyresult of the void data type.
Public actionresult Empty () { returnnull; }
The returned result is null.
(4). Httpunauthorizedresult: response to client error code 401 (unauthorized browsing state), if the program has enabled Forms authentication, and the client does not have any identity tickets, it jumps to the specified login page.
Public ActionResult Httpunauthorizedresult () { returnnew httpunauthorizedresult ();}
(5). Javascriptresult: Returns a script that Javascriptresult can execute on the client.
Public actionresult JavaScript () { stringstring. Format ("alert (' {0} '); " " pop-up window " ); return JavaScript (str); }
However, this does not directly respond to pop-up windows, which need to be called again using the page.
This makes it easy to perform different JS operations based on different logic.
(6). Jsonresult: Returns the JSON object Jsonresult serialized.
PublicActionResult Json () {Dictionary<string,Object> dic =Newdictionary<string,Object>(); Dic. ADD ("ID", -); Dic. ADD ("name","Hello"); returnJson (DIC, Jsonrequestbehavior.allowget); }
used primarily to return JSON-formatted objects, which can be manipulated using Ajax;Note: Parameters need to be set, Jsonrequestbehavior.allowget. Otherwise, an error will be prompted: This request has been blocked because sensitive information is disclosed to third-party Web sites when used in a GET request. to allow a GET request, set Jsonrequestbehavior to Allowget.
(7). Fileresult (Base): Returns the binary output of the Fileresult to write to the response, which can generally be used as a feature for simple download.
public ActionResult File () { string fileName = ~/content/test.zip ; // file name string downfilename = " file display name. zip ; // return File (FileName, application/octet-stream , Downfilename); }
The file display name. zip is saved to local when Test.zip is downloaded directly.
(8). Filecontentresult: Is the Fileresult for file content creation, It simply writes a byte array representing the contents of the file to the response output stream by calling the write method of the OutputStream property of the current HttpResponse.
(9). Filepathresult: is a Fileresult created based on the physical file path.
filestreamresult: Allows us to create a fileresult with a stream for reading the contents of a file.
PS: About the file of several return types of detailed introduction, you can refer to the blog " Jing Jinnan " to understand the nature of several actionresult of asp: Fileresult ", has been written in detail.
(one). redirectresult: go directly to the specified URL address.
Public actionresult Redirect () { // returns the specified URL address directly to return Redirect ("http// www.cnblogs.com"); }
redirecttorouteresult: Use the Action Name to jump directly or add controllername and parameters.
Public actionresult Redirectresult () { return redirecttoaction ("Index""Home " New " - " " Liu " });
redirecttoactionresult: Specifies the route to jump.
Public actionresult Redirectrouteresult () { return redirecttoroute ("Default"new" Home " " Index " });
Where default is the route name defined in Global.asax.cs.
(+). viewresultbase (base)
ViewResult: Returns the ViewResult view result and renders the view to the Web page.
Public ActionResult About () { return// parameter can return model object
(
partialviewresult: Returns partialviewresult partial view results, primarily for returning partial view content. Create a viewusercontrol.cshtml partial view under the View/shared directory.
Public actionresult UserControl () { " partial view "; return Partialview ("viewusercontrol");
Page call @viewbag.message will output "partial view".
Summary: The common denominator of these return types is that there are certain requirements for action:
- Must be a public method
- Must be an instance method
- Cannot be overloaded
- Must return ActionResult type