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 方法的参数类型总结。的更多相关文章

  1. C#--方法的参数类型

    在C#中,方法的参数类型有四种: 值类型 引用类型 输出类型 数组型参数 值参数: 所谓值参数,就是利用值向方法传递参数时,编译程序给实参的值做一份拷贝,并将此拷贝传递给该方法,这样做的结果就是被调用 ...

  2. 为什么 Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E ?

    前两天给同事做 code review,感觉自己对 Java 的 Generics 掌握得不够好,便拿出 <Effective Java>1 这本书再看看相关的章节.在 Item 24:E ...

  3. Java程序设计基础笔记 • 【第9章 方法与参数类型】

    全部章节   >>>> 本章目录 9.1 有参数有返回值的方法 9.1.1 有参数有返回值的方法的定义和调用 9.1.2 返回值的传递过程 9.1.3 方法返回值的注意事项 9 ...

  4. 关于PHP的方法参数类型约束

    在之前的文章PHP方法参数的那点事儿中,我们讲过关于PHP方法参数的一些小技巧.今天,我们带来的是更加深入的研究一下PHP中方法的参数类型. 在PHP5之后,PHP正式引入了方法参数类型约束.也就是如 ...

  5. Java在方法中定义可变参数类型

    学习目标: 掌握可变参数的应用 学习内容: 1.定义 在方法中传递数组有一种更简单的方式--方法的可变参数,其本质是一个语法糖,目的是让开发者写代码更简单. 2.语法 [修饰符] 返回值类型 方法名称 ...

  6. CLR via C# 读书笔记---常量、字段、方法和参数

    常量 常量是值从不变化的符号.定义常量符号时,它的值必须能在编译时确定.确定后,编译器将唱两只保存在程序集元数据中.使用const关键字声明常量.由于常量值从不变化,所以常量总是被视为类型定义的一部分 ...

  7. CLR via C#深解笔记四 - 方法、参数、属性

    实例构造器和类(引用类型) 构造器(constructor)是允许将类型的实例初始化为良好状态的一种特殊方法.构造器方法在“方法定义元数据表”中始终叫.ctor. 创建一个引用类型的实例时: #1, ...

  8. C#方法中参数ref和out的解析

    一.C#方法中参数类型 有4种参数类型,有时候很难记住它们的不同特征,下图对它们做一个总结,使之更容易比较和对照. 二.C#方法中的参数 1.值参数 使用值参数,通过复制实参的值到形参的方式把数据传递 ...

  9. c# 方法传递参数

    一.参数的使用方法: 1.值参数(Value Parameter ) 格式:方法名称(参数类型 参数名称[,参数类型 参数名称]) 2.引用参数(Reference Parameter ) 格式:方法 ...

随机推荐

  1. python 想搞加密算法吗?快戳这里

    加密算法介绍 一,HASH Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换 ...

  2. 【learning】kd-tree

    吐槽 kd-tree这个东西很早就听说过了但是qwq一直没有去了解 (原因的话..啊哈哈听说是什么跟二维平面之类的东西有关的所以就怂掉了qwq没错就是怂qwq) 但其实好像..真的很暴力啊qwq知道思 ...

  3. VMware下安装centos7及网络配置

    之前遇到过用虚拟机安装上centos7上不了网,昨天解决了,但是手抽删错了,把centos7误删了,今天就一起安装下. 首先打开VMware,我这里用的版本是VMware12,然后我们新建虚拟机 下一 ...

  4. 洛谷 P1146 【硬币翻转】题解

    很久很久之前做过的一道题 翻n-1枚硬币,就是有一枚不翻,也可以理解为翻一枚 直接上程序,看程序说话 #include<iostream> using namespace std; ; b ...

  5. autobase之配置文件

    配置文件内容: 1.db_info{},数据库链接属性,包括Oracle,dmdb,快立方 2.credit etc目录的路径 3.批量基础数据导入sql路径 4.用例执行日志存储目录路径 功能: 1 ...

  6. 【python学习笔记】6.抽象

    [python学习笔记]6.抽象 创建函数: 使用def语句定义函数,不用声明参数类型,和返回值类型 def function_name(param1, param2): 'this is docum ...

  7. ServiceFabric极简文档-0. ServiceFabric简介

    前言: 最近ServiceFabric开源了,大家热情都比较高,官方文档大而全,但快速入手不容易找到头绪.发几篇极简的文档,跟大家分享一下,顺便为Ray的ServiceFabric部署做一下铺垫.因为 ...

  8. python 练完这些,你的函数编程就ok了

    文件处理相关 1,编码问题 (1)请问python2与python3中的默认编码是什么? python 2.x默认的字符编码是ASCII,默认的文件编码也是ASCII python 2.x默认的字符编 ...

  9. 关于 Touchjs 手势识别事件库 this 关键字与选择器不对称情况

    Touchjs 版本 v0.2.14 废话不多,直接看代码,一个拖动实例 <div id="touch-drag"></div> <script ty ...

  10. Y2K问题

    关于第五章 团队和流程 2.6 特工团队中所提到的Y2K问题,第一次接触到这个名词去百度了,它的意思是这样的:year 2K problem,又称千年虫问题.主要原因是早期的软件大多以两位数字来记录年 ...