1.GET请求传递参数

URL传参:http://localhost/ApiTest/test?id=1

API接收参数

[HttpGet]

public string GetUser(int id) { return "User:" + id; }

传递JSON对象:

$.ajax({

type: "get",

url: "http://localhost/ApiTest/test",

contentType: "application/json",

data: { id: "1" },

success: function (data, status) { if (status == "success") { $("#div_test").html(data); } }

});

API接收参数

[HttpGet]

public string GetUser([FromUri]UserDto obj) { return "User:" + obj.id; }

如果你不想使用[FromUri]这些在参数里面加特性的这种“怪异”写法,也可以采用先序列化,再在后台反序列的方式。

$.ajax({
type: "get",
url: "http://localhost:27221/api/Charging/GetByModel",
contentType: "application/json",
data: { strQuery: JSON.stringify({ ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" }) },
success: function (data, status) {
if (status == "success") {
$("#div_test").html(data);
}
}
});

[HttpGet]

public string GetByModel(string strQuery)

{

TB_CHARGING oData = Newtonsoft.Json.JsonConvert.DeserializeObject<TB_CHARGING>(strQuery);

return "ChargingData" + oData.ID;

}

注意:方法名以Get开头,WebApi会自动默认这个请求就是get请求,而如果你以其他名称开头而又不标注方法的请求方式,那么这个时候服务器虽然找到了这个方法,但是由于请求方式不确定,所以直接返回给你405——方法不被允许的错误。

POST传参:

单个参数:

$.ajax({
type: "post",
url: "http://localhost:27221/api/Charging/SaveData",
data: { "": "Jim" },
success: function (data, status) {}
});
[HttpPost]
public bool SaveData([FromBody]string NAME){return true;}

多个参数,使用dynamic:

$.ajax({
type: "post",
url: "http://localhost:27221/api/Charging/SaveData",
contentType: 'application/json',
data: JSON.stringify({ NAME: "Jim",DES:"备注" }),
success: function (data, status) {}
});
[HttpPost]
public object SaveData(dynamic obj){var strName = Convert.ToString(obj.NAME);return strName;}

传递集合:

var arr = [
{ ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" },
{ ID: "2", NAME: "Lilei", CREATETIME: "1990-12-11" },
{ ID: "3", NAME: "Lucy", CREATETIME: "1986-01-10" }
];
$.ajax({
type: "post",
url: "http://localhost:27221/api/Charging/SaveData",
contentType: 'application/json',
data: JSON.stringify(arr),
success: function (data, status) {}
});

[HttpPost]
public bool SaveData(List<TB_CHARGING> lstCharging)
{
return true;
}

来源:http://www.cnblogs.com/landeanfen/p/5337072.html

WebAPI传参的更多相关文章

  1. WebApi传参总动员(四)

    前文介绍了Form Data 形式传参,本文介绍json传参. WebApi及Model: public class ValuesController : ApiController { [HttpP ...

  2. C# WebApi传参之Post请求-AJAX

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    上一节讲述了C# WebApi传参之Get请求 ...

  3. WebApi传参总动员(一)

    目前自己的工作和WebApi相关,免不了传入.接收参数.以前的老办法是从请求流中获取json,再反序列化,这中间有2个不能控制的地方,一个是流,一个是反序列化,都需要try,总感觉非常的不爽.因此对W ...

  4. WebApi传参总动员(五)

    上回说到涉及多个实体的传参,用常规的方法已经不能解决了.这回我们用终极大招搞定她. WebApi:注意要引用JSON.Net [HttpPost] public string GetData(stri ...

  5. WebApi传参总动员(三)

    上篇介绍了如何从输入流中获取实体对象.本篇介绍以url形式传递参数.简单的参数不再赘述,这里主要实现形如(string name,Woman woman)这样的参数传递. 本篇及后面几章均涉及js调用 ...

  6. C# WebApi传参之Get请求-AJAX

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    在介绍本篇博客之前,先来温故下AJax的请求, ...

  7. C#进阶系列——WebApi 传参详解

    本篇打算通过get.post.put.delete四种请求方式分别谈谈基础类型(包括int/string/datetime等).实体.数组等类型的参数如何传递. 回到顶部 一.get请求 对于取数据, ...

  8. WebApi传参总动员(填坑)

    本以为系列文章已经Over,突然记起来前面留了个大坑还没填,真是自己给自己挖坑. 这个坑就是: (body 只能被读取一次)Only one thing can read the body MVC和W ...

  9. c# 调用webapi 传参 特殊字符的问题

    最近在做对接数据接口,遇到一些问题,在C#后台写请求webapi的接口,但是传递过程中参数如果有特殊字符,传入过去之后又问题. 需要转换一下,通过System.Web.HttpUtility.UrlE ...

随机推荐

  1. TypeScript语言学习笔记(2)

    接口 // 在参数类型中定义约束 function printLabel(labelledObj: { label: string }) { console.log(labelledObj.label ...

  2. LeetCode OJ 79. Word Search

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  3. Android DevArt1:假设当前Activity为A,如果这时用户打开一个新的Activity B,那么B的onResume和A的onPause哪个先执行呢?

    问题描述:假设当前Activity为A,如果这时用户打开一个新的Activity B,那么B的onResume和A的onPause哪个先执行呢? GitHub Demo 废话少说,上代码,Activi ...

  4. C#调用Delphi接口(ITest = interface)

    首先创建一个delphi的DLL工程 library testintfdll; { Important note about DLL memory management: ShareMem must ...

  5. git 恢复到旧版本命令

    1.第一步:找到你想恢复到的版本号:可以在git提交日志中查看-> 找到版本号,复制下来,在git项目根目录下打开git命令窗口: 输入:git reset --hard xxxxxxxxxxx ...

  6. Structs复习 Action传递参数

    Structs传递参数通常有三种方式 下面我来一个个介绍 1.属性 Jar包 web.xml <?xml version="1.0" encoding="UTF-8 ...

  7. Java解决输出数组问题

    package test; public class doubleshuzu { public static void main(String[] args) { double a[][]; Stri ...

  8. IE 浏览器的兼容性列表设置

    打开 IE 浏览器 IE8 为例如:

  9. k8s 创建deployment流程

    pod 创建流程https://blog.csdn.net/yan234280533/article/details/72567261 api server -> etcd -> cont ...

  10. JDBC事物的处理

    JDBC事物的处理: 概念:事务是指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 数据库开启事务命令: start transaction  开启事务 Rollback   ...