NET core model bindings are different from previous versions

Source: Internet
Author: User
Tags form post unsupported

Previously there was a callback URL for the seven-ox upload image (previously used by the. net4.0, which works)

The code is as follows:

        //Seven Ox callback address, callbackbody content Name=upload/member/1.jpg&hash=fn6qeqi4vdlq347nirm-rlqx_4o2         Public ObjectPost ([Frombody]Dynamicdata) {ILog Logger=Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod ().            DeclaringType); Logger. Info ("Data:"+data.            ToString ()); intStatus =-1; Try            {                stringAvator_url =Data.name; intStartpos ="upload/member/".                Length; stringmember_id = Avator_url. Substring (Startpos, Avator_url. IndexOf (". jpg") -startpos);                Memberhelper.updateavator (member_id, Avator_url); varJSON =New{results =New string[0], status =0 }; returnJSON; }            Catch(Exception ex) {stringmsg = ex. Message +Ex.                StackTrace; Logger. Error ("\ r \ n Data:"+ data. ToString () +"\r\nexception:"+msg, ex); varJSON =New{results =New string[0], status = status, MSG =msg}; returnJSON; }        }

The same code now prompts HTTP 415 error in. NET Core – Unsupported media type (Unsupported media types)

Please refer to this article: https://www.cnblogs.com/CreateMyself/p/6246977.html

It is mentioned in the net Mvc/webapi that the Controller has the ability to bind the HTTP request body either as a form post or as a JSON, and the data is returned to us, and we do not need to make any special representations.

seven the URL format of the ox callback should be in Form post (contentType: "application/x-www-form-urlencoded").

The. NET core MVC model bindings, the default parameter binding type is Fromform

Fromquery, corresponding to the urlencoded string ("? key1=value1&key2=value2") in the URL. Fromform, corresponding to the urlencoded string ("Key1=value1&key2=value2") in the request content. Frombody, corresponding to the JSON string in the request content ("{" Key1 ":" Value1 "," Key2 ":" Value2 "}"). 

. NET Core is strictly limited, Post ([frombody]dynamic data) This notation, must correspond to the parameter type is the JSON format (contentType: "Application/json"), otherwise there will be a 415 error /c0>

All we can do is write 2 methods, no matter how the seven cows use that method to call, can return correctly.

Seven Bull callback address, callbackbody content name=upload/member/memberid.jpg&hash=FN6QEQI4VDLQ347NIRM-RLQX_4O2 [allowanonymous] [HttpPost ("Updateavatorjson")] public object Post ([Fro            Mbody]dynamic data) {int status =-1;                try {updateavator (data.name);                var json = new {results = "", status = 0};            return JSON; } catch (Exception ex) {string msg = ex. Message + ex.                StackTrace;                var json = new {results = "", Status = status, MSG = MSG};            return JSON; }} [AllowAnonymous] [HttpPost ("Updateavatorform")] public object Post (string name, string have            h) {int status =-1;                try {updateavator (name);                var json = new {results = "", status = 0};            return JSON; } catch (Exception ex) {string msg = ex. Message + ex.          StackTrace;      var json = new {results = "", Status = status, MSG = MSG};            return JSON; }        }

NET core model bindings are different from previous versions

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.