WebApi接口访问频率控制的实现 2021-08-27

1、使用Nuget,对WebAPI项目添加WebApiThrottle的引用

2、进行注册,一般是在WebApiConfig的Register方法里添加,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebApiThrottle;

namespace nnn
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //1、使用Nuget,对WebAPI项目添加WebApiThrottle的引用

            //2、进行注册,一般是在WebApiConfig的Register方法里添加,代码如下:

            config.Filters.Add(new ThrottlingFilter()
            {
                Policy = new ThrottlePolicy()
                {
                    //ip配置区域
                    IpThrottling = true,
                    ClientThrottling = true,

                    //端点限制策略配置会从EnableThrottling特性中获取。
                    EndpointThrottling = true
                }
            });
        }
    }
}

3、对需要控制的接口或者控制器加上头标示 

  [EnableThrottling(PerMinute = 12)]//控制访问频率,每分钟最多12次

不需要控制访问频率的可以不加或者加上

 [DisableThrotting]

这样就实现了使用过滤器控制特定API的访问频率,更多的使用方法可以参考下面的地址。

参考资料:

https://blog.csdn.net/weixin_30817749/article/details/98903913

WebApiThrottle限流框架使用手册  http://www.cnblogs.com/mushroom/p/4659200.html

控制ASP.NET Web API 调用频率与限流 http://www.cnblogs.com/Irving/p/4664786.html

控制ASP.NET Web API 调用频率 http://www.cnblogs.com/shanyou/p/3194802.html

转载于:https://www.cnblogs.com/SzeCheng/p/5407316.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值