.netcore webapi的返回值和过滤器

1.返回值。

1.1直接返回数据

 

 

 1.2 数据+状态码

 返回这种类型IActionResult

可以使用return OK(T)、return NotFound(T)

 

1.3前两种的混合使用ActionResult 

可以接收前两种的返回值参数

2.过滤器(方法过滤器为例)

过滤器是一种AOP(面向切面编程)技术的体现。有两种注册方式,1是全局注册在ConfigureServices中注册。2.控制器或者方法上用属性注册

            services.AddControllers(cfg =>
            {
                cfg.Filters.Add(typeof(APISignAttribute));
            });
复制代码
    [APISignAttribute]
    public class ContractController : ControllerBase
    {
        public IConfiguration Configuration { get; }
        public ContractController(IConfiguration _configuration)
        {
            Configuration = _configuration;
        }
 
        [HttpGet]
        [ClientIpCheckFilter]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
复制代码

 

posted @   海爬sir  阅读(467)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示