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

推荐订阅源

博客园 - Franky
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Hacker News
The Hacker News
F
Full Disclosure
有赞技术团队
有赞技术团队
H
Help Net Security
Security Latest
Security Latest
P
Palo Alto Networks Blog
MyScale Blog
MyScale Blog
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
P
Privacy International News Feed
P
Proofpoint News Feed
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
罗磊的独立博客
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Latest news
Latest news
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
L
LINUX DO - 热门话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
Lohrmann on Cybersecurity
O
OpenAI News
S
SegmentFault 最新的问题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 柠茶

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-06 · 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}]