WebApi 方法的参数类型总结。
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";//可以跳转.
}
});
};
WebApi 方法的参数类型总结。的更多相关文章
- C#--方法的参数类型
在C#中,方法的参数类型有四种: 值类型 引用类型 输出类型 数组型参数 值参数: 所谓值参数,就是利用值向方法传递参数时,编译程序给实参的值做一份拷贝,并将此拷贝传递给该方法,这样做的结果就是被调用 ...
- 为什么 Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E ?
前两天给同事做 code review,感觉自己对 Java 的 Generics 掌握得不够好,便拿出 <Effective Java>1 这本书再看看相关的章节.在 Item 24:E ...
- Java程序设计基础笔记 • 【第9章 方法与参数类型】
全部章节 >>>> 本章目录 9.1 有参数有返回值的方法 9.1.1 有参数有返回值的方法的定义和调用 9.1.2 返回值的传递过程 9.1.3 方法返回值的注意事项 9 ...
- 关于PHP的方法参数类型约束
在之前的文章PHP方法参数的那点事儿中,我们讲过关于PHP方法参数的一些小技巧.今天,我们带来的是更加深入的研究一下PHP中方法的参数类型. 在PHP5之后,PHP正式引入了方法参数类型约束.也就是如 ...
- Java在方法中定义可变参数类型
学习目标: 掌握可变参数的应用 学习内容: 1.定义 在方法中传递数组有一种更简单的方式--方法的可变参数,其本质是一个语法糖,目的是让开发者写代码更简单. 2.语法 [修饰符] 返回值类型 方法名称 ...
- CLR via C# 读书笔记---常量、字段、方法和参数
常量 常量是值从不变化的符号.定义常量符号时,它的值必须能在编译时确定.确定后,编译器将唱两只保存在程序集元数据中.使用const关键字声明常量.由于常量值从不变化,所以常量总是被视为类型定义的一部分 ...
- CLR via C#深解笔记四 - 方法、参数、属性
实例构造器和类(引用类型) 构造器(constructor)是允许将类型的实例初始化为良好状态的一种特殊方法.构造器方法在“方法定义元数据表”中始终叫.ctor. 创建一个引用类型的实例时: #1, ...
- C#方法中参数ref和out的解析
一.C#方法中参数类型 有4种参数类型,有时候很难记住它们的不同特征,下图对它们做一个总结,使之更容易比较和对照. 二.C#方法中的参数 1.值参数 使用值参数,通过复制实参的值到形参的方式把数据传递 ...
- c# 方法传递参数
一.参数的使用方法: 1.值参数(Value Parameter ) 格式:方法名称(参数类型 参数名称[,参数类型 参数名称]) 2.引用参数(Reference Parameter ) 格式:方法 ...
随机推荐
- ajax 状态码
状态码定义 ... 10 信息1xx ... 10.1 100继续 ... 10.1.1 101交换协议 ... 10.1.2 成功的2xx ... 10.2 200 OK ... 10.2.1 20 ...
- springboot dubbo filter之依赖注入null
@Autowiredprivate ICallerRepository callerRepository;...用dubbo提供的ServiceBean即可获取bean,因为该类已经实现了Applic ...
- PHP 获取访问来源
原文:http://www.upwqy.com/details/16.html $_SERVER['HTTP_REFERER'] 通过这个全局变量可以获取访问的链接是来源于哪里 比如说从博客园 htt ...
- python爬微信公众号前10篇历史文章(4)-正则表达式RegularExpressionPattern
正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串.将匹配的子串替换或者从某个串中取出符合某个条件的子串等. Pytho ...
- SoupUI安装
(仅供学习,严禁用于商业应用) 今天开始在公司需要做websocket接口测试,以前从来没有接触过,对美好生活充满了向往啊,今天从安装开始吧! 至于什么是websocket自行百度,我也是百度出来的, ...
- C语言第十一次作业--函数嵌套调用
一.实验作业 1.1 PTA题目:递归法对任意10个数据按降序排序 设计思路 定义整型循环变量i,最小值下标min,中间变量t 若n==1,直接返回 否则 min=10-n 最小值下标赋初值 for ...
- jar包和war包的介绍与区别
在学习maven的过程中接触到了jar包和war包.之前在写小项目的时候真的遇到过war包,当时为了找到jar包,把war包 的后缀名改成了.rar的压缩文件,在里面提取出来jar包来用.其实jar包 ...
- JQ在光标处插入文字
内容转载自网络这是一个JQ的扩展方法.在teatarea获得焦点时,往光标处插入文字,扩展代码如下 (function($){ $.fn.extend({ "insert":fun ...
- 用Canvas写一个简单的游戏--别踩白块儿
第一次写博客也不知怎么写,反正就按照我自己的想法来吧!怎么说呢?还是不要扯那些多余的话了,直接上正题吧! 第一次用canvas写游戏,所以挑个简单实现点的来干:别踩白块儿,其他那些怎么操作的那些就不用 ...
- JAVA代码提示
经过以上过程,整个项目需要的环境差不多搭建完成了.接下来一个小技巧,如下图进行配置之后就可以将只在.出现时进行代码提示换成任意字母+.出现时的代码提示了(.abcdefghijklmnopqrstuv ...