WebApi 方法的参数类型总结。
2024-10-19 13:19:13
1:[HttpGet]
①:get方法之无参数。
[HttpGet]
public IHttpActionResult GetStudentInfor()
{
List<StudentModel> stlists = new List<StudentModel>();
stlists.Add(new StudentModel { hno = "", hname = "龙大炳", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大炳", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大炳", hobject = "SignalR", hscore = "" });
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 = "", hname = "龙大炳", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "longdb", hobject = "SignalR", hscore = "" });
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 = "", hname = "龙大炳", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "longdb", hobject = "SignalR", hscore = "" });
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: "", hname: "龙大炳", hobject: "SignalR", hscore: "" };
$.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 = "", hname = "龙", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "longdb", hobject = "SignalR", hscore = "" });
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 = "", hname = "龙", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "longdb", hobject = "SignalR", hscore = "" });
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: "", hname: "longdb", hobject: "SignalR", hscore: "" };
$.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 = "", hname = "龙", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "longdb", hobject = "SignalR", hscore = "" });
StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st[]); 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 = "", hname = "龙", hobject = "WebApi", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "龙大", hobject = "Ajax", hscore = "" });
stlists.Add(new StudentModel { hno = "", hname = "longdb", hobject = "SignalR", hscore = "" });
StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == st[].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";//可以跳转.
}
});
};
最新文章
- CSS知识总结(六)
- 深入理解javascript选择器API系列第二篇——getElementsByClassName
- FMDB
- Oracle truncate和delete的区别
- 用jquery判断当前显示器的分辨率,加载不同CSS
- java web实现读取指定盘符下的图像(二)
- SQLServer中临时表与表变量的区别分析【转】
- enode框架step by step之消息队列的设计思路
- WinForm 控件(下)
- Bootstrap按钮插件
- 关闭eclipse自动弹出console的功能
- 配置samba的流程
- python———day01
- datasnap isapi程序iis设置
- 与HTTP关系密切的三个协议:IP,TCP,DNS
- 关于dos命令行脚本编写
- Lodop导出excel及提示成功【回调和直接返回值】
- Codeforces 950 D. A Leapfrog in the Array
- 升级 php composer 版本
- Centos 创建 docker项目