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";//可以跳转.
             }
         });
     };

 

转载于:https://www.cnblogs.com/longdb/p/7820436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值