我正在尝试向我的 Web Api 应用程序添加使用限制。但是,未实现自定义属性。
自定义属性
using System;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
namespace MyApp.Filters
{
public enum TimeUnit
{
Minute = 60,
Hour = 3600,
Day = 86400
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ThrottleAttribute : ActionFilterAttribute
{
public TimeUnit TimeUnit { get; set; }
public int Count { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
...
}
}
}
控制器
namespace MyApp.Controllers
{
public class GetUsersController : ApiController
{
[Throttle(TimeUnit = TimeUnit.Minute, Count = 5)]
[Throttle(TimeUnit = TimeUnit.Hour, Count = 20)]
[Throttle(TimeUnit = TimeUnit.Day, Count = 100)]
public ICollection<Users> Get(int id)
{
...
}
}
}
我走远了吗?不正确地实现属性?扩展错误的属性?我知道我正在使用System.Web.Mvc而不是System.Web.Http.Filters......但我所看到的所有资源都要求这样做。也许你有更好的答案?:)
慕哥6287543
FFIVE
相关分类