netcore v2-方法過濾器的使用

/*******************************HeaderFiterAttribute.cs**************************************/

using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PocoDemo
{
    public class HeaderFiterAttribute:ActionFilterAttribute
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Value))
            {
                context.HttpContext.Response.Headers.Add(Name, Value);
            }
            base.OnActionExecuted(context);
        }
    }
}
/***************************TestController.cs**************************************************/

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PocoDemo
{
    public class TestController:Controller
    {
        [HeaderFiter(Name ="Test",Value ="Index")]
        public IActionResult Index()
        {
            return Content("just verify action filter");
        }
    }
}
 

發佈了459 篇原創文章 · 獲贊 71 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章