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. C#多线程编程序--聊聊线程

    这篇文章主要给您讲解几个基本问题,什么是线程?为什么要用线程?线程万能?这几个问题.我这篇博客是在该系列另外几篇博客之后写的,之所以不往下继续写,而是回到最初的问题,是因为我感觉到我没有很好的讲解开头 ...

  2. Hadoop集群环境安装

    转载请标明出处:  http://blog.csdn.net/zwto1/article/details/45647643:  本文出自:[zhang_way的博客专栏] 工具: 虚拟机virtual ...

  3. go语言的数组和切片区别

    这里不介绍数组和切片的使用技巧,主要看下2者的区别. 首先看下它们的定义: 数组:类型 [n]T 表示拥有 n 个 T 类型的值的数组. 切片:类型 []T 表示一个元素类型为 T 的切片. 看一个数 ...

  4. NGUI_Depth

    四.深度(Depth)概念; 1. (1).每一个UIPanel和每一个UI控件都一定会有一个Depth,深度值大代表显示的优先级高(会趋向于在界面更上层显示) (2).Depth决定的是UI的显示层 ...

  5. Gauge----自动化测试工具--使用

    开始吧 1 下载安装gauge(根据官网教程 http://getgauge.io/documentation/user/current/)测试:gauge -v step01 磁盘上新建一个空目录- ...

  6. Android TextView数字增长动画效果

    某些app上,新进入一个Activity的时候,上面的一个关键性数字(比如金额)会以一个数字不断变大的动画来显示.刚开始的时候,想到的一个方案是:使用Thead+Handler,给定一个动画总时长与刷 ...

  7. MySQL使用和操作总结

    简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...

  8. Matlab绘图基础——绘制三维曲线

    %% 绘制三维曲线 %plot3函数,其中每一组x,y,z组成一组曲线的坐标参数,选项的定义和plot函数相同. %1.当x,y,z是同维向量时,则x,y,z 对应元素构成一条三维曲线. x0 = 0 ...

  9. 配置COCO API(安装COCO)

    仍旧是win10,Python3.5 从GitHub下载coco源码,解压到任意文件夹.(或者创建一个工程)coco源码链接 https://github.com/cocodataset/cocoap ...

  10. java数组排序,并将数组内的数据求和

    java数据编列并求和,江湖我狼哥,人狠话不多,直接上代码! import java.util.Arrays; public class Intarry { public static void ma ...