WebApi 方法的參數類型總結。


1:[HttpGet]

 ①:get方法之無參數。

     [HttpGet]
        public IHttpActionResult GetStudentInfor()
        {
            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "龍大炳", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大炳", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "龍大炳", hobject = "SignalR", hscore = "88" });
            return Json<List<StudentModel>>(stlists);
        }

 

Client,Ajax調用。

function Sumittomain() {

$.ajax({ url: 'http://192.168.0.102/webApiDemo/api/WebApiTest/GetStudentInfor',//'/api/WebAPITest/GetString',
contentType: 'application/json;charset=utf-8',
type: 'get', ////數據類型必須有 //dataType: "text", 
async: true,//異步
success: function (data) //成功后的回調方法
{ alert(JSON.stringify(data))
//彈出框

alert(JSON.stringify(stuentmodel))//彈出框
window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
},
error:
function () {
alert(
"失敗!");
window.location.href
= "EasyUILoutMain.aspx";//可以跳轉. }
});
}

②:get方法之基礎參數。

        /// <summary>
        /// Get,一個基礎參數。
        /// </summary>
        /// <param name="hname"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetStudentInforBasePara(string hno,string hname)
        {
            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "龍大炳", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" });
            StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == hname);

            return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

Client,Ajax調用。

     //get,基礎數據做參數。參數放在uri后,或者data中都可以,但最終還是把參數串接在uri中。by ldb 2017.11.13 20:18
     //get請求的數據會附在URL之后(就是把數據放置在HTTP協議頭中),而post請求則是放在http協議包的包體中。
     function GetStudentInforBasePara() {
         $.ajax({
             url: 'http://localhost/webApiDemo/api/WebApiTest/GetStudentInforBasePara',//?hno=1001&hname=longdb',
             type: 'get',
             //contentType: 'application/json',//有無都可以。
             data: {hno:"1001",hname: "龍大炳"},//{ hno: "1001", hname: "龍大炳", hobject: "SignalR", hscore: "80" },
             async: true,//異步
             //crossDomain: true,
             success: function (data) //成功后的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
                 //window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             }
         });
     };

③:get方法之實體參數。

        /// <summary>
        /// get,實體參數。[FromUri]加了也取不到Client傳過來的參數。
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetStudentInforModelParaUri([FromUri]StudentModel st)
        {
            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "龍大炳", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" });
            StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st.hname);

            return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

 

Client,Ajax調用。

     //get,實體參數,不報錯,但是后台取不到傳過去的參數。
     function GetStudentInforModelParaUri() {
         var studentmodel = { hno: "1001", hname: "龍大炳", hobject: "SignalR", hscore: "80" };
         $.ajax({
             url: 'http://localhost/webApiDemo/api/WebApiTest/GetStudentInforModelParaUri',//?hno=1001&hname=longdb',
             type: 'get',
             data: studentmodel,//
             async: true,//異步
             success: function (data) //成功后的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
             }
         });
     };

④:get方法之實體參數轉換成JSon。

        /// <summary>
        /// get,實體參數轉換成JSon。
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetStudentInforModelParaJSON(string stuJSON)
        {
            //把JSON轉換成StudentModel對象。
            StudentModel st = Newtonsoft.Json.JsonConvert.DeserializeObject<StudentModel>(stuJSON);

            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "龍大炳", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" });
            StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st.hname);

            return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

 

Client,Ajax調用。

     //get,實體先轉換成json.成功。
     function GetStudentInforModelParaJSON() {
         var studentmodel = { hno: "1001", hname: "龍大炳", hobject: "SignalR", hscore: "80" };
         $.ajax({
             type: 'get',
             url: 'http://localhost/webApiDemo/api/WebApiTest/GetStudentInforModelParaJSON',
             contentType: "application/json",
             data: { stuJSON: JSON.stringify(studentmodel) },//
             async: true,//異步
             success: function (data) //成功后的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
             }
         });
     };

 

2:[HttpPost]

①:ApiController中方法參數類型之單個參數。

      /// <summary>
        /// post,一個參數。用[FromBody]去http的請求體里面去取參數。 /// Client請求成功 /// </summary>
        /// <param name="hname"></param>
        /// <returns></returns>
 [HttpPost] public IHttpActionResult PostStudentInforOnePara([FromBody]string hname) { List<StudentModel> stlists = new List<StudentModel>(); stlists.Add(new StudentModel { hno = "1001", hname = "", hobject = "WebApi", hscore = "90" }); stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" }); stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" }); StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == hname); return  Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

Client 中Ajax方式調用:

     //POST WebApi之一個參數的方法。成功
     function SumittomainPostOne() { $.ajax({ url: 'http://192.168.0.102/webApiDemo/api/WebApiTest/PostStudentInforOnePara', type: 'post', data: { "": "longdb" },//一個參數時,必須這樣寫,webapi中http的請求體里面去取參數才能取到。 
             async: true,//異步
             success: function (data) //成功后的回調方法
 { alert(JSON.stringify(data))//彈出框 
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
 }, error: function () { alert("失敗!"); window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
 } }); }

 ②://post webapi,方法參數之實體類型。

     /// <summary>
        /// post,實體作為參數。
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult PostStudentInforModel(StudentModel st)
        {
            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" });
            StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st.hname);

            return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

Client,Ajax調用api.

     //post webapi,實體類型,能成功,但是參數傳不到api中。
     function PostStudentInforModelPara() {
         var studentmodel = { hno: "1001", hname: "longdb", hobject: "SignalR", hscore: "80" };
         $.ajax({
             url: "http://localhost/webApiDemo/api/WebApiTest/PostStudentInforModel",
             type: "post",
             //contentType: "application/json",
             data: studentmodel,
             async: true,//異步
             success: function (data) //成功后的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
                 //window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             }
         });
     };

 

③:post,方法之數組。

       /// <summary>
        /// post,數組(localhost--成功。ip測試不行,估計是跨域的問題。)。
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult PostStudentInforArray(string[] st)
        {
            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" });
            StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st[1]);

            return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

Client,Ajax調用。

     //post webapi,數組類型.localhost情況成功,改成固定ip就不行了,跨域的原因??
     function PostStudentInforArraryPara() {
         var studentarr = ["1001", "龍大", "SignalR", "80"];
         $.ajax({
             url: 'http://localhost/webApiDemo/api/WebApiTest/PostStudentInforArray',
             type: 'post',
             contentType: 'application/json',
             data: JSON.stringify(studentarr),
             async: true,//異步
             //crossDomain: true,
             success: function (data) //成功后的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
                 //window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             }
         });
     };

 

④:post方法之集合。

       /// <summary>
        /// 集合。
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult PostStudentInforList(List<StudentModel> st)
        {
            List<StudentModel> stlists = new List<StudentModel>();
            stlists.Add(new StudentModel { hno = "1001", hname = "", hobject = "WebApi", hscore = "90" });
            stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80" });
            stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" });
            StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st[0].hname);

            return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
        }

 

Client,Ajax調用。

     //post webapi,集合。localhost情況成功,改成固定ip就不行了(SCRIPT7002: XMLHttpRequest: 網絡錯誤 0x80070005, 拒絕訪問。),跨域的原因??
     function PostStudentInforListPara() {
         var studentarr = [
                { hno: "1001", hname: "龍", hobject: "SignalR", hscore: "80" },
                { hno: "1001", hname: "龍大", hobject: "SignalR", hscore: "80" },
                { hno: "1001", hname: "longdb", hobject: "SignalR", hscore: "80" }
               ];
         $.ajax({
             url: 'http://localhost/webApiDemo/api/WebApiTest/PostStudentInforList',
             type: 'post',
             contentType: 'application/json',
             data: JSON.stringify(studentarr),
             async: true,//異步
             //crossDomain: true,
             success: function (data) //成功后的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
                 //window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             }
         });
     };

 


免責聲明!

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



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