net core 模型繫結與之前版本的不同

來源:互聯網
上載者:User

標籤:hash   \n   tor   返回   style   query   text   exception   creat   

之前有一個用於七牛上傳圖片的Callback Url的WebAPI (之前是用.net4.0,運行正常)

代碼如下:

        // 七牛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;            }        }

現在同樣的代碼在.net Core 卻提示 HTTP 415 錯誤 – 不支援的媒體類型(Unsupported media type)

請參考這篇文章: https://www.cnblogs.com/CreateMyself/p/6246977.html

裡面提到 ASP.NET MVC/WebAPi中無論是以表單POST的形式抑或JSON的形式控制器具有綁定都Http請求Body的能力同時資料都會返回給我們,我們不需要做出任何特別的說明

七牛CallBack的URL格式應該是用表單POST的形式(contentType: "application/x-www-form-urlencoded").

.net core mvc 模型繫結,預設的參數綁定類型是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 則嚴格限制了, Post([FromBody]dynamic data)  這個寫法, 必須對應參數類型是JSON格式( contentType: "application/json"),否則會出現415錯誤  

我們可以做的就是寫2個方法,無論七牛用那個方法去調用,都能正確返回.

 

        // 七牛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;            }        }

 

net core 模型繫結與之前版本的不同

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.