Fileresult,javascriptresult,jsonresult

Source: Internet
Author: User
Tags actionlink

Fileresult: You can respond to any document's properties, including binary format data, eg: files, pdf,excel,zip, can pass in byte[], document Path, stream, and so on, allowing MVC to pass properties back to the client, in addition to You can also specify the property category at the time of the callback, or specify the file name to display when the client downloads.

Actually divided into three: Filepathresult: responds to an attribute of an entity document Filecontentresult: Responds to a byte[] property. Filestreamresult: Responds to a stream property. The file helper method can automatically select the different Fileresult responses above. Eg:r If you want to output a PNG file that is placed in the App_Data directory by action, public ActionResult GetFile () {return file (Server.MapPath Usera/avatar.png ")," Image/png ");} If you want the browser to download the file directly instead of opening the file directly in the browser, you can also pass in the required download document? In the third parameter, such as a PDF document from the database, and want to let the user download, you can first obtain a byte[] or stream data, and specify the correct content-type in the second parameter of the file helper, and finally specify the document name to download Eg:public actionresult GetFile () {byte[] filecontent= Getfilebytearray/fromdb (); return File (Filecontent, "Application/pdf", "yourreport.pdf");//The third parameter document name can be Chinese// But MVC is a RFX2331 specification to set Chinese encoding, and the RFC2231 specification does not support the encoding format of HTTP Header value for many older browsers (IE6, older versions of Safari and Chrome). So the old version of the Chinese name to be processed so return to File (filecontent, "Application/pdf", Server.urlpathencode ("your report. txt"));(only valid for IE)} When the user clicks the URL of the action, a prompt is given to download the file. Javascriptresult: In response to the JavaScript program code to the browser, through the development of AJAX programs, you can use the Javascriptresult to respond to the appropriate code to allow the browser to run dynamically, In fact, Javascriptresult function and Contentresult almost, the main difference is the default content-type is not the same, javascriptresult the default content-type for application /x-javascript. Eg:public actionresult JavaScript () {return JavaScript ("alert (' OK ')");} Use the Ajax helper method in view to compose: @Ajax. ActionLink ("Run JavaScript", "JavaScript", New Ajaxoptions ()) Note: Run this program in the MVC4 default network project template. Remember in the head tag on the motherboard page _layout.cshtml load the correct JS library to function properly @aja.actionlink helper method@Scripts. Render ("~/bundles/jquery") @Scripts. Render ("~/bundles/jqueryval") Jsonresult: JavaScript Object notation is a data format used by the Web to implement AJAX applications, and Jsonresult automatically serialize arbitrary object data to JSON format. Jsonresult default contenttype is Application/json,For some javascriptframework this is a necessary requirement, Eg:jqueryjsonresult is to use JavaScriptSerializer to complete the JSON serial operation, but if your object cannot be serialized, the process of this conversion will be the exception. Note: In order to avoid jsonhijacking attacks, MVC security-based considerations do not allow HTTP get to get any JSON information by default for any Jsonresult callback request. Eg:public ActionResult JSON () {return JSON (new {id=1,name= "would", Createdon=datetime.now});} Use HttpPost to get: {"id": 1, "name": "Would",... "} often use JQ dynamically to obtain JSON data, $.getjson in JQ is to use the Get method to dynamically obtain JSON data, If the Jsonresult does not have a thermal setting, it will result in the inability to obtain JSON information normally, and there is no built-in $.postjson in JQ, but it is very easy to implement the demo provided Jquery.post official website: $.postjson=function (Url,data, Callback) {$.post (Url,data,callback, "JSON");} You can use this new $.postjson method, the same as Getjson, you can refer to the http://api.jquery.com/jQuery.getJSON/summary: For security reasons, try to avoid using HttpGet to get JSON data , but using only httppost to get JSON also has the problem that the data retrieved from the server can not be cached by the browser, if your information is not sensitive and want to operate the cache, you may also need to let jsonresult to httpget requirements corresponding, The solution is to add a Jsonrequestbehavior enumeration parameter to the JSON helper method, so that the JSON data can be obtained through get. Eg:public ActionResult JSON () {return JSON (new {id=1,name= "would", Createdon=datetime.now,} Jsonrequestbehavior.allowget);}

Fileresult,javascriptresult,jsonresult

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.