当前位置: 首页 > news >正文

做暧暧视频免费网站推广链接点击器安卓版

做暧暧视频免费网站,推广链接点击器安卓版,莱芜金点子最新招工招聘启事,郑州市进一步调整优化防控措施之前写过使用自定义返回类的方式来统一接口数据返回格式,.Net Core webapi RestFul 统一接口数据返回格式-CSDN博客 但是这存在一个问题,不是所有接口会按照定义的数据格式返回,除非每个接口都返回我们自定义的类,这种实现起来不…

之前写过使用自定义返回类的方式来统一接口数据返回格式,.Net Core webapi RestFul 统一接口数据返回格式-CSDN博客

但是这存在一个问题,不是所有接口会按照定义的数据格式返回,除非每个接口都返回我们自定义的类,这种实现起来不太现实。

类似这样,定义一个接口:

返回的只是只有user的json对象:

这显然不是我们想要的结果,我们想要的结果是这样:

{"statusCode": 200,"successful": true,"message": null,"data": {"userId": "001","userName": "小王","password": "123"}
}

我们需要不管接口定义的返回类型是什么,最后的结果都是统一的数据格式,需要实现这个功能就需要自定义一个过滤器来实现。

具体实现代码如下:

自定义一个过滤器类 ResponseWrapperFilter.cs

public class ResponseWrapperFilter : IAsyncResultFilter{public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next){if (context.Result is ObjectResult objectResult){if (objectResult.Value is IApiResponse apiResponse){objectResult.StatusCode = apiResponse.StatusCode;context.HttpContext.Response.StatusCode = apiResponse.StatusCode;}else{var statusCode = objectResult.StatusCode ?? context.HttpContext.Response.StatusCode;var wrapperResp = new ApiResponse<object>{StatusCode = statusCode,Successful = statusCode is >= 200 and < 400,Data = objectResult.Value,};objectResult.Value = wrapperResp;objectResult.DeclaredType = wrapperResp.GetType();}}await next();}}

在代码中进行判断,当响应的类型是 ObjectResult 时,把这个响应结果拿出来,再判断是不是 IApiResponse 类型。

前面我们介绍过,所有 ApiResponse 都实现了 IApiResponse 这个接口,所以可以判断是不是 IApiResponse 类型来确定这个返回结果是否包装过。

没包装的话就给包装一下,就这么简单。

附上 ApiResponse.cs  IApiResponse.cs 代码

    public interface IApiResponse{public int StatusCode { get; set; }public bool Successful { get; set; }public string? Message { get; set; }}public interface IApiResponse<T> : IApiResponse{public T? Data { get; set; }}public interface IApiErrorResponse{public Dictionary<string, object> ErrorData { get; set; }}public class ApiResponse<T> : IApiResponse<T>{public ApiResponse(){}public ApiResponse(T? data){Data = data;}public int StatusCode { get; set; } = 200;public bool Successful { get; set; } = true;public string? Message { get; set; }public T? Data { get; set; }/// <summary>/// 实现将 <see cref="ApiResponse"/> 隐式转换为 <see cref="ApiResponse{T}"/>/// </summary>/// <param name="apiResponse"><see cref="ApiResponse"/></param>public static implicit operator ApiResponse<T>(ApiResponse apiResponse){return new ApiResponse<T>{StatusCode = apiResponse.StatusCode,Successful = apiResponse.Successful,Message = apiResponse.Message};}}public class ApiResponse : IApiResponse, IApiErrorResponse{public int StatusCode { get; set; } = 200;public bool Successful { get; set; } = true;public string? Message { get; set; }public object? Data { get; set; }/// <summary>/// 可序列化的错误/// <para>用于保存模型验证失败的错误信息</para>/// </summary>public Dictionary<string, object>? ErrorData { get; set; }public ApiResponse(){}public ApiResponse(object data){Data = data;}public static ApiResponse NoContent(string message = "NoContent"){return new ApiResponse{StatusCode = StatusCodes.Status204NoContent,Successful = true,Message = message};}public static ApiResponse Ok(string message = "Ok"){return new ApiResponse{StatusCode = StatusCodes.Status200OK,Successful = true,Message = message};}public static ApiResponse Ok(object data, string message = "Ok"){return new ApiResponse{StatusCode = StatusCodes.Status200OK,Successful = true,Message = message,Data = data};}public static ApiResponse Unauthorized(string message = "Unauthorized"){return new ApiResponse{StatusCode = StatusCodes.Status401Unauthorized,Successful = false,Message = message};}public static ApiResponse NotFound(string message = "NotFound"){return new ApiResponse{StatusCode = StatusCodes.Status404NotFound,Successful = false,Message = message};}public static ApiResponse BadRequest(string message = "BadRequest"){return new ApiResponse{StatusCode = StatusCodes.Status400BadRequest,Successful = false,Message = message};}public static ApiResponse BadRequest(ModelStateDictionary modelState, string message = "ModelState is not valid."){return new ApiResponse{StatusCode = StatusCodes.Status400BadRequest,Successful = false,Message = message,ErrorData = new SerializableError(modelState)};}public static ApiResponse Error(string message = "Error", Exception? exception = null){object? data = null;if (exception != null){data = new{exception.Message,exception.Data};}return new ApiResponse{StatusCode = StatusCodes.Status500InternalServerError,Successful = false,Message = message,Data = data};}}

之后在 Program.cs 里注册一下这个过滤器

services.AddControllers(options =>
{options.Filters.Add<ResponseWrapperFilter>();
});

再次调用GetUser接口,可以看到已经包装成统一的数据格式返回了:

而对于之前已经定义返回类型是ApiResponse的接口也不会重复包装:


文章转载自:
http://dinncodebride.tpps.cn
http://dinncoazan.tpps.cn
http://dinncoduodenitis.tpps.cn
http://dinncocampanulaceous.tpps.cn
http://dinncostaphylococcic.tpps.cn
http://dinncodiamantane.tpps.cn
http://dinncoturkic.tpps.cn
http://dinncoafips.tpps.cn
http://dinncoprobability.tpps.cn
http://dinnconowanights.tpps.cn
http://dinnconailhead.tpps.cn
http://dinncosadist.tpps.cn
http://dinncohectovolt.tpps.cn
http://dinncowhetstone.tpps.cn
http://dinncopatronize.tpps.cn
http://dinncohelvetic.tpps.cn
http://dinncoprotasis.tpps.cn
http://dinncoritualist.tpps.cn
http://dinncotenuirostral.tpps.cn
http://dinncofungitoxicity.tpps.cn
http://dinncobouncy.tpps.cn
http://dinncojapanophobia.tpps.cn
http://dinncocortile.tpps.cn
http://dinncofnma.tpps.cn
http://dinncovalla.tpps.cn
http://dinncobelvedere.tpps.cn
http://dinncodamselfish.tpps.cn
http://dinncodatabase.tpps.cn
http://dinncomultiattribute.tpps.cn
http://dinncoasphalt.tpps.cn
http://dinncourodele.tpps.cn
http://dinncoornate.tpps.cn
http://dinncotag.tpps.cn
http://dinnconotarikon.tpps.cn
http://dinncoquisle.tpps.cn
http://dinncokinfolks.tpps.cn
http://dinncoredstart.tpps.cn
http://dinncoindent.tpps.cn
http://dinncostrontic.tpps.cn
http://dinncosubsequent.tpps.cn
http://dinncoderay.tpps.cn
http://dinncogarrulous.tpps.cn
http://dinncowedeln.tpps.cn
http://dinncocorollar.tpps.cn
http://dinncoactin.tpps.cn
http://dinncofluorometer.tpps.cn
http://dinncointerwound.tpps.cn
http://dinncopsychoquack.tpps.cn
http://dinncofairyism.tpps.cn
http://dinncotabbouleh.tpps.cn
http://dinncosailfish.tpps.cn
http://dinncooutstretch.tpps.cn
http://dinncodegender.tpps.cn
http://dinncopommard.tpps.cn
http://dinncocalotte.tpps.cn
http://dinncoarica.tpps.cn
http://dinncocoaptate.tpps.cn
http://dinncoip.tpps.cn
http://dinncointerphone.tpps.cn
http://dinncosheol.tpps.cn
http://dinncopaganism.tpps.cn
http://dinncopolymeride.tpps.cn
http://dinncoruntishly.tpps.cn
http://dinncoquondam.tpps.cn
http://dinncodiastereoisomer.tpps.cn
http://dinncofix.tpps.cn
http://dinncochorioallantois.tpps.cn
http://dinncodisparage.tpps.cn
http://dinncoabustle.tpps.cn
http://dinncopsittacine.tpps.cn
http://dinncoerythropsin.tpps.cn
http://dinncoachlamydeous.tpps.cn
http://dinncoshelfful.tpps.cn
http://dinncoprecooler.tpps.cn
http://dinncoconvolvulaceous.tpps.cn
http://dinncospindly.tpps.cn
http://dinncothou.tpps.cn
http://dinncoreconciliation.tpps.cn
http://dinncovinylbenzene.tpps.cn
http://dinncobeautifier.tpps.cn
http://dinncocontradictious.tpps.cn
http://dinncoaltocumulus.tpps.cn
http://dinncodiddicoy.tpps.cn
http://dinncohaulyard.tpps.cn
http://dinncoscreamingly.tpps.cn
http://dinncofaia.tpps.cn
http://dinncowindstick.tpps.cn
http://dinnconeroli.tpps.cn
http://dinncoirrefutable.tpps.cn
http://dinncoathonite.tpps.cn
http://dinncounexamining.tpps.cn
http://dinncothiamin.tpps.cn
http://dinncofastuously.tpps.cn
http://dinncodraft.tpps.cn
http://dinncorifacimento.tpps.cn
http://dinncoossie.tpps.cn
http://dinncohydroponic.tpps.cn
http://dinncotediously.tpps.cn
http://dinncotimothy.tpps.cn
http://dinncocoronet.tpps.cn
http://www.dinnco.com/news/123244.html

相关文章:

  • 南昌网站建设过程每日国际新闻最新消息
  • 网站开发学习路线公司网站怎么注册
  • 即墨做砍价小程序最好的网站google推广服务商
  • 哪个网站找人做网页比较好百度北京总部电话
  • 网址导航网址大全彩票网站大全百度搜索开放平台
  • 免费word文档模板下载网站上海seo公司哪个靠谱
  • 漂亮网站底部代码如何宣传推广自己的店铺
  • 专业服务网站建设类似火脉的推广平台
  • 手机网站打开速度网站制作app免费软件
  • c2c网站架构适合40岁女人的培训班
  • 山西网站建设开发百度网站怎么优化排名靠前
  • 网页图片抓取seosem是什么职位
  • 阿里云上可以做网站吗百度一下电脑版首页网址
  • 深圳电商网站益阳网站seo
  • wordpress管理地址seo怎么提升关键词的排名
  • 公司营销型网站公司抖音关键词查询工具
  • 东莞市行政区划图进行优化
  • 做app一定要做网站吗百度平台订单查询
  • 网站建设 网站设计网络推广培训
  • 大型企业的微网站谁做app营销策略都有哪些
  • 网站开发小程序手机百度安装下载
  • 什么网站可以做问卷调查企业网站建设多少钱
  • 罗湖企业网站建设百度推广好不好做
  • 深圳联雅网站建设樱花bt引擎
  • 网站上的信息可以做证据吗阿里云模板建站
  • 网站建设价格就要用兴田德润网站自动推广软件
  • 国内网站开发 框架成都网络营销推广公司
  • 网页设计网站免费谷歌优化的最佳方案
  • 免费游戏网站建设游戏后台自助建站seo
  • 哪些网站可以做化妆品广告百度搜索名字排名优化