How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?
ou can customize the JsonSerializerSettings
by using theFormatters.JsonFormatter.SerializerSettings
property in the HttpConfiguration
object.
For example, you could do that in the Application_Start() method:
protected void Application_Start()
{
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.Formatting =
Newtonsoft.Json.Formatting.Indented;
}
//---------------------------------------------------
控制ASP.NET Web API 调用频率
很多的api,例如GitHub’s API 都有流量控制的做法。使用速率限制,以防止在很短的时间量客户端向你的api发出太多的请求.例如,我们可以限制匿名API客户端每小时最多60个请求,而我们可以让更多的经过认证的客户端发出更多的请求。那么asp.net webapi如何实现这样的功能呢?在项目WebApiContrib 上面已经有了一个实现:https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/MessageHandlers/ThrottlingHandler.cs ,具有良好的可扩展性。
最简单的方法是使用ThrottlingHandler注册使用简单的参数,例如控制每个用户每小时60个请求:
config.MessageHandlers.Add(new ThrottlingHandler(
new InMemoryThrottleStore(),
id => 60,
TimeSpan.FromHours(1)));
IThrottleStore接口 使用ID +当前的请求数量。InMemoryThrottleStore 只有一个内存中存储,但你可以轻松地扩展实现为分布式缓存或数据库。还可以轻松地自定义ThrottlingHandler的行为,例如我们针对一个ip地址可以更好的进行控制。
How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?的更多相关文章
- On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
Ints are easy. Strings are mostly easy. Dates? A nightmare. They always will be. There's different c ...
- Replace JSON.NET with Jil JSON serializer in ASP.NET Web API
I have recently come across a comparison of fast JSON serializers in .NET, which shows that Jil JSON ...
- .Net MVC 4 Web Api 输出Json 格式
1.Global 中增加json输出 GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add( ...
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- ASP.NET Web Api返回对象类型为JSON还是XML
在Umbraco平台上开发过程中,我用WebApi返回JSON result给前端 前端使用React调用这个web api来获取JSON result 我写的web api方法是返回JSON 类型的 ...
- ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...
- .net mvc web api 返回 json 内容,过滤值为null的属性
原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...
- C# .net mvc web api 返回 json 内容,过滤值为null的属性
在WebApiConfig.Register 中增加一段 #region 过滤值为null的属性 //json 序列化设置 GlobalConfiguration.Configuration.Form ...
- [转] JSON Web Token in ASP.NET Web API 2 using Owin
本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...
随机推荐
- Inno Setup使用上的几个问题 (转)
Inno Setup使用上的几个问题: [问题一:Inno Setup 执行REG文件代码?][Run]Filename: "{win}\regedit.exe";Paramete ...
- js 中的switch
前言 switch 这种表达式在很多语言中都有,比如java, C等待, 使用switch比使用if else 来得方便,来得清晰. 使用语法很简单: switch(n) { case 1: 执行代码 ...
- openGL 初试 绘制三角形 和添加鼠标键盘事件
code: #include <gl/glut.h> #include <stdlib.h> void render(void); void keyboard(unsigned ...
- MFC Attach()函数和Detach()函数
一.Windows对象和MFC对象的区别?MFC对象实际上并没有把整个Windows对象都包装在其中.对于窗口:MFC对象它只是有一个窗口句柄而已,这个窗口句柄如果指向一个实际存在的窗口对象(窗口对象 ...
- hibernate 非xml实体类配置方法!
hibernate 非xml实体类配置方法! 这个是hibernate.cfg.xml配置文件 <?xml version='1.0' encoding='UTF-8'?> <!DO ...
- tomcat root dir log 配置
tomcat 配置log记录及root 目录
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- iOS 自我检測
1.id 和 NSObject的差别? 2.UITableViewCell的复用原理? 3.UIView生命周期和UILayer的差别? 4.多线程NSOperation和Queue.GDC.Thre ...
- AspectJ教学
这几天看JAVA基础看的有点头疼,决定时不时的换换口味,准备開始调研一些如今流行的技术,于是,開始埋头思考自己知识的盲区(当时,自己的知识盲区茫茫多...),想了几天后,决定要開始研究一下几种技术及实 ...
- Java File类读取文件属性
package myjavademo;import java.io.*; publicclass MyJavaDemo { public static void main(String[] ...