惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
美团技术团队
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
L
LangChain Blog
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
腾讯CDC
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
博客园 - 司徒正美
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
C
Check Point Blog
小众软件
小众软件
V
Visual Studio Blog
V
V2EX
F
Full Disclosure
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
人人都是产品经理
人人都是产品经理
量子位
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
博客园_首页
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
Recorded Future
Recorded Future
G
Google Developers Blog
Vercel News
Vercel News
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
爱范儿
爱范儿
Jina AI
Jina AI

博客园 - 柠茶

asp.net webapi 参数绑定总结 javascript引用类型 javascript变量,作用域和内存问题 javascript基本概念 javascript中所谓的“坑”收录 JS调用webservice的两种方式 反射体验(转) 什么是Emit,什么是反射,二者区别到底是什么?(转) 关于可空类型 学习IIS & MVC的运行原理 (转) MVC-READ5(asp.net web from PK asp.net MVC) MVC-READ4 MVC-READ3(视图引擎主要类关系图) MVC-READ2 MVC-READ1 @@ERROR和@@ROWCOUNT的用法 异常以及异常处理框架探析(转) .Net深入学习序列化和反序列化 (转) 可扩展对象模式(转)
asp.net webapi 参数绑定总结
柠茶 · 2016-04-16 · via 博客园 - 柠茶

 首先必须得更正下自己一直以来对于get请求和post请求理解的一个误区:get请求只能通过url传参,post请求只能通过body传参。

其实上面的理解是错误的,翻阅了不少资料及具体实践,正确理解应该是:get和post是http协议(规范)定义的和服务器交互的不同方法,get用于从服务器获取资源(安全和幂等),post用于修改服务器上的资源。传参方式和请求方式没有任何关系,get和post请求既可以接受url传参,也可以接收body传参,取决于服务端的参数绑定机制。

OK,回到主题,webapi参数绑定最佳实践,直接上例子:

1:简单类型参数绑定&url传参:

服务端:

[HttpGet]
[HttpPost]
public HttpResponseMessage FinnTest(string name,int age)
{
var json = "{name:'" + name + "',age:" + age + "}";
var resp = new HttpResponseMessage
{
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
return resp;
}

客户端调用调用:

post http://localhost:27984/HjkDealer/FinnTest?name=finn&age=88 http/1.1

2:简单类型参数绑定&body传参

服务端:

[HttpGet]
[HttpPost]
public HttpResponseMessage FinnTest2([FromBody]string name, int age)
{
var json = "{name:'" + name + "',age:" + age + "}";
var resp = new HttpResponseMessage
{
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
return resp;
}

客户端调用:

post http://localhost:27984/HjkDealer/FinnTest2?age=88 http/1.1
content-type:application/x-www-form-urlencoded;charset=utf-8

=finn

注意红色标注:content-type头部必须,否则name参数不能正确接收;body体如果写成:“name=finn”或“finn”,name参数也不能正确接收,深层次原因尚不清楚

3:复杂类型参数绑定&body传参(复杂类型默认body传参)

public class fiona
{
public string name { get; set; }

public string age { get; set; }
}

[HttpGet]
[HttpPost]
public HttpResponseMessage FinnTest4(fiona fiona)
{
var json = "{name:'" + fiona.name + "',age:" + fiona.age + "}";
var resp = new HttpResponseMessage
{
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
return resp;
}

 客户端调用:

post http://localhost:27984/HjkDealer/FinnTest4 http/1.1
content-type:application/x-www-form-urlencoded;charset=utf-8

name=finn&age=88

4:List复杂类型参数绑定&body的json方式传参(复杂类型默认body传参)

服务端:

[HttpGet]
[HttpPost]
public HttpResponseMessage FinnTest5(List<fiona> fi)
{
var json = "{name:'" + fi[0].name + "',age:" + fi[0].age + "}";
var resp = new HttpResponseMessage
{
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
return resp;
}

客户端调用:

post http://localhost:27984/HjkDealer/FinnTest5 http/1.1
content-type:application/json

[{name:"finn",age:88},{name:"fiona",age:99}]