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地址可以更好的进行控制。
 

转载于:https://www.cnblogs.com/fx2008/p/3210005.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值