C# webapi post方法參數值為null


webapi

public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }
        [HttpPost]
        // POST api/values
        public void Post([FromBody]string value)
        {
            XTrace.WriteLine("values:" +value);
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }

  

(1)表單提交方式

<html>
    <head>
        <title>測試表單</title>
    </head>
    <body>
        <form id="myForm" action="/api/values" method="post">
            <input type="text" value="hhh" placeholder="請輸入你的名字" />
            <input type="submit" value="提交"/>
            <!--<input type="submit" value="提交"/>-->
        </form>
        <script>
            //var form = document.getElementById("myForm");
            
        </script>
    </body>
</html>

 后台無法獲取到value的值,一直為null。將[FromBody]屬性修改為[FromUri]即可以獲取到參數的值,默認form提交表單是通過url傳參的,詳細了解了解一下webapi的Frombody和FromUri做了什么工作,以及ajax提交方式,怎樣寫是對應着body,怎樣寫是對應着uri。

(2)使用Fiddler發起post請求

  webapi 屬性設置為[FromBody],設置Content-Type為application/json,在RequestBody中輸入鍵值對,可以成功post並獲取到對象。若不設置Content-Type,但設置RequestBody,http返回415錯誤。 

public void Post([FromBody] string value)
        {
            XTrace.WriteLine("values:"+value);
        }

 webapi屬性設置為[FromUri],通過url中傳入參數值post可以成功獲取到.

 

 public void Post([FromUri] string value)
        {
            XTrace.WriteLine("values:"+value);
        }

  

由此,可以推測出html中使用form提交表單時,在屬性為[FromBody]時,要成功訪問參數,需要設置content-type格式。問題是,使用form標簽時,只能設置encrypt屬性(提交數據的格式),

 1 application/x-www-form-urlencoded: 概述: 當action為get,數據被編碼為名稱/值對,是標准的編碼格式,也是默認的編碼格式 格式:name1=value1&name2&value2 把form數據轉換成一個字串,然后把這個字串append到url后面,用?分割,加載這個新的url

2 multipart/form-data: 概述:當action為post時,瀏覽器把form數據封裝到http body中,然后發送到server。 如果沒有type=file的控件,用默認的application/x-www-form-urlencoded就可以了。 但是如果有type=file的話,就要用到multipart/form-data了。瀏覽器會把整個表單以控件為單位分割,並為每個部分加上ContentDisposition(form-data或者file),Content-Type(默認為text/plain),name(控件name)等信息,並加上分割符(boundary)。file或者img等發生上傳文件時,設置entype = 'multipart/form-data',是上傳二進制數據,它告訴我們傳輸的數據要用到多媒體傳輸協議,由於多媒體傳輸的都是大量的數據,所以規定上傳文件必須是post方法,<input>的type屬性必須是file。form里面的input的值以2進制的方式傳過去,所以request就得不到值了。

 可以看到,可以通過multipart/form-data的方式封裝數據到http body中,然后后台進行解析,問題是默認的Content-Type是text/plain,而不是application/json。存疑:不能通過form標簽指定Content-Type為application/json,只能通過ajax指定?。順理成章地,應該可以提交ajax請求,指定Content-Type為application/json來獲取數據。奇怪的是,這種ajax提交方式獲取不到參數值。

<html>
    <head>
        <title>測試表單</title>
    </head>
    <body>
        <form>
            <button type="button" id="submit">提交</button>
        </form>
        <script>
            var button = document.getElementById("submit");
            button.onclick = function () {
                $.ajax({
                    url: "/api/values",
                    method: "post",
                    contentType: "application/json",
                    dataType:"json",
                    data: JSON.stringify("hhh"),
                });
            }
        </script>
    </body>
</html>

  

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM