ActionFilter和ResultFilter是ASP.NET MVC框架中的两种过滤器类型,用于在控制器的执行过程中进行全局的处理和修改。
public class CustomAuthorizationFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// 身份验证逻辑
if (!User.Identity.IsAuthenticated)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
public class CustomResultFilter : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
// 结果缓存逻辑
var cacheKey = "ResultCacheKey";
var cachedResult = Cache.Get(cacheKey);
if (cachedResult != null)
{
filterContext.Result = cachedResult;
}
}
}
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云