net core model bundled with previous versions of different -FromBody must correspond Json format

Original: NET Core model binding with different -FromBody previous versions must correspond Json format

Before there is a WebAPI for seven cattle upload pictures Callback Url of (previously with .net4.0, normal operation)

code show as below:

        // 七牛CallBack地址,CallbackBody内容name=upload/member/1.jpg&hash=Fn6qeQi4VDLQ347NiRm-RlQx_4O2
        public object Post([FromBody]dynamic data)
        {
            ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            logger.Info("data:" + data.ToString());
            int status = -1;

            try
            {
                string avator_url = data.name;
                int startPos = "upload/member/".Length;
                string member_id = avator_url.Substring(startPos, avator_url.IndexOf(".jpg") - startPos);
                MemberHelper.UpdateAvator(member_id, avator_url);
                var json = new { results = new string[0], status = 0 };
                return json;

            }
            catch (Exception ex)
            {
                string msg = ex.Message + ex.StackTrace;
                logger.Error("\r\n data:" + data.ToString() + "\r\nException:" + msg, ex);
                var json = new { results = new string[0], status = status, msg = msg };
                return json;

            }


        }

Now the same code in .net Core has prompted HTTP 415 error - Unsupported media types (Unsupported media type)

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

Which referred to  ASP.NET MVC / WebAPi in the form POST whether in the form of JSON or whether the form of the controller has the ability to bind all Http requests Body simultaneous data will be returned to us, we do not need to make any special instructions

Seven cattle CallBack URL format should be used in the form of a form POST ( contentType: "the Application / the X--the WWW-form-urlencoded" ).

 

.net core mvc model binding, the default parameter binding type is FromForm

 

FromQuery,对应 url 中的 urlencoded string ("?key1=value1&key2=value2")。


FromForm,对应 request content 中的 urlencoded string("key1=value1&key2=value2")。


FromBody,对应 request content 中的 JSON string("{"key1":"value1","key2":"value2"}")。

.NET Core is severely restricted,  Post ([FromBody] the Data Dynamic) this wording, must correspond to the parameter type is JSON format (contentType: "application / json" ), otherwise there will be 415 error  

We can do is write two methods, both with seven cattle to call that method, you can get back correctly.

 

        // 七牛CallBack地址,CallbackBody内容name=upload/member/memberId.jpg&hash=Fn6qeQi4VDLQ347NiRm-RlQx_4O2
        [AllowAnonymous]
        [HttpPost("updateAvatorJSON")]
        public object Post([FromBody]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 hash)
        {
            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;

            }
        }

 

 

********** Postscript ---- When micro letter applet wx.request, again encountered similar problems *************

Client:

    wx.request({
      url: url,
      method: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      data: dataParam,
      success: function (res) {

      }})

Corresponding server-side code must [FromForm] modified, or fail to get the data. Since only find default .net core QueryString

 [HttpPost]
        public dynamic Post(            
            [FromForm]string CustomOrderNumber,
            [FromForm]int CustomerId
            )
        {}

------------------------------------------------------------------------------------------------------

If a client with a "Content-Type": "application / json", the server must use [FromBody]

 

------------------------------------------------------------------------------------------------------

Server [FromForm] and [FromBody] can not be mixed, api interfaces or to a large object, or to make other arguments put QueryString in

        public dynamic Post(            
            string CustomOrderNumber,
            int CustomerId,
            [FromBody]OrderItemDTO[] Items
            )

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11125534.html