zoukankan      html  css  js  c++  java
  • webapi自定义Filter

    public class MyAutorFilter : IAuthorizationFilter
    {
    public bool AllowMultiple => true;

    public async Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
    IEnumerable<string> UserNames;
    if (!actionContext.Request.Headers.TryGetValues("UserName", out UserNames))
    {
    return new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
    }
    string username = UserNames.First();
    if (username=="admin")
    {
    return await continuation();
    }
    else
    {
    return new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
    }

    }
    }

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

    还有ActionFilter  ExceptionFilter

    apiResult 用法

    public class ApiResult<T>
    {
    public int Code { get; set; }
    public string Message { get; set; }
    public T ReturnValue { get; set; }
    }

    public ApiResult<string> Get(int id)
    {
    if (id<0)
    {
    return new ApiResult<string> { Code = 1, Message = "年龄太小" };
    }
    else if(id>100)
    {
    return new ApiResult<string> { Code = 2, Message = "年龄太da" };
    }
    else
    {
    return new ApiResult<string> { Code = 0, ReturnValue="Hellp" };
    }
    }

  • 相关阅读:
    Python语言之并发编程
    python语言之系统工具
    python语言之正则
    python语言之字符串与字节
    Python语言之持久化
    Python语言之数字格式化与时间
    Python语言之异常处理与测试
    Java-AQS源码详解(细节很多!)
    redis的主从复制原理
    Amdahl定律和可伸缩性
  • 原文地址:https://www.cnblogs.com/yingchuanxiaoge/p/9136770.html
Copyright © 2011-2022 走看看