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 ) 格式:方法 ...
随机推荐
- HiHocoder1419 : 后缀数组四·重复旋律4&[SPOJ]REPEATS:Repeats
题面 Hihocoder Vjudge Sol 题目的提示说的也非常好 我对求\(LCP(P - L + len \% l, P + len \% L)\)做补充 \(len=LCP(P, P + L ...
- Mac下redis的安装 以及配置支持PHP使用redis
1 下载最新redis https://redis.io/download 2 安装redis. 这部分在上面下载链接中 官网提供的有相关操作 如下: $ wget http://download ...
- php文件上传原理详解(含源码)
1.文件上传原理 将客户端的文件上传到服务器,再将服务器的临时文件上传到指定目录 2.客户端配置 提交表单 表单的发送方式为post 添加enctype="multipart/form-da ...
- devstack部署openstack环境
背景:公司需要搭建openstack私有云.配置两台物理服务器. 各大搜索引擎了解了下OpenStack.决定先在虚拟机上部署实现openstack. 前提准备 设备:一台宿主机Windows10 1 ...
- C语言与C++语言之间关系
很多时候我们对于C和C++的区别不是很清楚,以至于弄混的情况并不少见.那C语言和C++语言到底是怎么回事呢? 首先,我们来看下百度百科对语言和C++语言描述,相对而说也还算是比较权威的. C语言 C语 ...
- mysql安装过程
1.到官网下载Mysql,目前最新版都是5.0以上版本,下载之后直接解压即可 2.在终端进入bin目录(如果嫌麻烦可配置环境变量,配置之后则无需进入bin目录则可敲命令),安装数据库服务:my ...
- 设计模式——中介者模式/调停者模式(C++实现)
#include <iostream> #include <string> using namespace std; class Colleague; class Mediat ...
- Cesium 一个导致浏览器内存一直增长的方法
为了实时更改模型的位置,给模型附上ID,后面判断如果传来的数据中没有已经创建的模型,删掉该模型时用到方法:viewer.entities.removeById(modelId);和viewer.ent ...
- JQ在光标处插入文字
内容转载自网络这是一个JQ的扩展方法.在teatarea获得焦点时,往光标处插入文字,扩展代码如下 (function($){ $.fn.extend({ "insert":fun ...
- es6使用技巧
##1.通过参数默认值实现强制参数 ES6 的参数默认值只有在真正使用时才会求值.这可以让你强制确保提供参数: /** * Called if a parameter is missing and * ...