使用工具:

Googl  浏览器+PostMan 插件

写了个  控制器

添加了个Action,结果呢?GET 方式请求没问题。

POST一直,在服务器端获取不了参数。。。找了官方的文档 。各种雨里雾里。

总而言之,post 方式,推荐将数据封闭到一个类中,然后整体提交Json数据,或者Xml数据。首选是Json

POST提交表单

1 键值对的方式

2 Body 的方式

其中 键值对的方式 又分为  URI的键值对  ,form-data的键值对,application/x-www-form-urlencoded的键值对

Body的方式,又根据 Content-Type 分为不同的类型 《json ,xml,text/html.......》

其实好乱。。。就用json 即可。

使用json 的时候,发送的时候 ,使用的编码集,请首选 UTF-8...因为。他就支持 UTF8 UNICODE俩编码集

var encodings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedEncodings;

//config.Formatters.JsonFormatter.SupportedEncodings.Clear();
//config.Formatters.JsonFormatter.SupportedEncodings.Add(System.Text.Encoding.Default);

不信自己试试去吧。。。。

简易参数,必须 int string   。使用frombody。。。标注。。。。最好不要使用简易类型,你会很蛋疼。不信试试。。。。

frombody的对立是 fromUri。。。。。

额 。。。

参考自定义 JSON 格式化器,在web API中。

my previous post I wrote about Web API and the content negotiation. In this post I’d like to describe how it’s easy to change the Json serialization.

The most important improvement from the Beta to the RC in the Web API is about the serialization, in fact now the framework is using Json.Net for the serialization. For all people who don’t know what Json.Net is I can say, to make the definition easily, Json.Net is the best serializer in the .NET world.

It's cool for many reasons, especially because it's flexible and easy to setup. In this post I’d like to show the principals and useful settings that you probably want to change in your application, or can be helpful for you in the future.

Before to start, we have to make our Web API project easy to debug so, I’m going to remove the XML formatter. I’m doing that because I’m in a test project and I’d like to see the response in the browser. The easily way to force the response to Json for all Web API requests is to remove the XML. Obviously you shouldn't do it in production.

NOTE: All code in this post,except the last one, must be located into the global.asax.cs

var formatters = GlobalConfiguration.Configuration.Formatters;

formatters.Remove(formatters.XmlFormatter);

Now we can start change the setting for all Json responses accessing toGlobalConfiguration.Configuration.Formatters.JsonFormatter.

In the following examples I’ll use always the class below:

public class User
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Username { get; set; }
public DateTime Birthdate { get; set; }
public Uri Website { get; set; }
public int Age { get; set; }
public double Salary { get; set; } [JsonIgnore]
public string IgnoreProperty { get; set; }
}

How can we indent the json response?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

How can we change the case in the response?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

How can we manage the null in the response?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

How can we change the DateTime format?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;

How can we change the TimeZone format?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

How can we change the Culture of the serializer?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.Culture = new CultureInfo("it-IT");

Another cool feature of Web API is to opportunity to override the configuration for a single response. You can use all of the previous setting directly in the single action like explained in the code below:

public HttpResponseMessage Get(){
IList<User> result = new List<User>();
result.Add(new User
{
Age = 34,
Birthdate = DateTime.Now,
Firstname = "Ugo",
Lastname = "Lattanzi",
IgnoreProperty = "This text should not appear in the reponse",
Salary = 1000,
Username = "imperugo",
Website = new Uri("http://www.tostring.it")
}); var formatter = new JsonMediaTypeFormatter();
var json =formatter.SerializerSettings; json.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
json.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.Formatting = Newtonsoft.Json.Formatting.Indented;
json.ContractResolver = new CamelCasePropertyNamesContractResolver();
json.Culture = new CultureInfo("it-IT"); return Request.CreateResponse(HttpStatusCode.OK, result, formatter);
}

Such as ASP.NET MVC, Web API are really flexible ... and ROCKS!

参考文献:

http://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-1

http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

http://www.cnblogs.com/zgynhqf/p/3180527.html

http://tostring.it/2012/07/18/customize-json-result-in-web-api/

https://dotblogs.com.tw/yc421206/archive/2013/11/11/127593.aspx#webrequest-post

使用 asp.net Web API 2的坑的更多相关文章

  1. (一)Asp.net web api中的坑-【找不到与请求 URI匹配的 HTTP 资源】

    在.net下,创建一个HTTP服务,有很多方案,比较老ashx,一般处理程序(HttpHandler), Web Service SOAP协议的,数据格式是XML,HTTP协议         WCF ...

  2. (四)Asp.net web api中的坑-【api的返回值】

    void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 我这里并不想赘述这些返回类型, 可以参考博文http://blog.csdn.net/leon ...

  3. (三)Asp.net web api中的坑-【http post请求中的参数】

    接上篇, HttpPost 请求 1.post请求,单参数 前端 var url = 'api/EnterOrExit/GetData2';var para = {};para["Phone ...

  4. (二)Asp.net web api中的坑-【http get请求中的参数】

    webapi主要的用途就是把[指定的参数]传进[api后台],api接收到参数,进行[相应的业务逻辑处理],[返回结果].所以怎么传参,或者通俗的说,http请求应该怎么请求api,api后台应该怎么 ...

  5. ASP.NET Web API实现微信公众平台开发(一)服务器验证

    最近朋友的微信公众号准备做活动,靠固定的微信公众平台模版搞定不了,于是请我代为开发微信后台.鉴于我也是第一次尝试开发微信后台,所以也踩了不少坑,此系列博客将会描述微信公众号各项功能的实现. 先决条件 ...

  6. ASP.NET Web API + Elasticsearch 6.x 快速做个全文搜索

    最近想做个全文搜索,设想用 ASP.NET Web API + Elasticsearch 6.x 来实现. 网上搜了下 Elasticsearch 的资料,大部分是讲 linux 平台下如何用 ja ...

  7. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  8. ASP.NET Web API Model-ActionBinding

    ASP.NET Web API Model-ActionBinding 前言 前面的几个篇幅把Model部分的知识点划分成一个个的模块来讲解,而在控制器执行过程中分为好多个过程,对于控制器执行过程(一 ...

  9. ASP.NET Web API Model-ParameterBinding

    ASP.NET Web API Model-ParameterBinding 前言 通过上个篇幅的学习了解Model绑定的基础知识,然而在ASP.NET Web API中Model绑定功能模块并不是被 ...

随机推荐

  1. 提高效率的便签By番茄时间管理 win7标签,小功能,大作用

    今日待办 把一些重要的事情,列入其中. 着重处理. 活动清单 罗列一些最近需要做的事情,不一定按照紧急重要的程度. 把活动清单中的事情,按照实际情况,安排到今日待办当中. 还有一个我喜欢的'头脑风暴' ...

  2. wikioi1369 xth 砍树

    题目描述 Description 在一个凉爽的夏夜,xth 和 rabbit 来到花园里砍树.为啥米要砍树呢?是这样滴, 小菜儿的儿子窄森要出生了.Xth这个做伯伯的自然要做点什么.于是他决定带着 r ...

  3. Hdu3498-whosyourdaddy(精确覆盖模板题)

    Problem Description sevenzero liked Warcraft very much, but he haven't practiced it for several year ...

  4. hdu 5611 Baby Ming and phone number(模拟)

    Problem Description Baby Ming collected lots of cell phone numbers, and he wants to sell them for mo ...

  5. 一个简陋的 CSS 样式

    有些网友对 Smart Framewok 中的 Sample 示例的样式比较感兴趣.由于本人对前端不太精通,但为了满足网友们的需求,只好献丑了. 以下这个简陋的 CSS 样式: ? 1 2 3 4 5 ...

  6. C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...

  7. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6666491 在前面一篇文章Android系统匿 ...

  8. BOOST 线程完全攻略 - 扩展 - 事务线程

    扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...

  9. Flexbox属性可视化指南

    Flexbox 布局(国内很多人称为弹性布局)正式的全称为 CSS Flexible Box布局模块,它是CSS3新增的一种布局模式.它可以很方便地用来改善动态或未知大小的元素的对齐,方向和顺序等等. ...

  10. TNS-00512: Address already in use-TNS-12542: TNS:address already in use

    监听启动或是停止时提示如下错误:TNS-12542: TNS:address already in use TNS-12560: TNS:protocol adapter error TNS-0051 ...