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

新闻网站界面设计怎么在腾讯地图上添加自己的店铺

新闻网站界面设计,怎么在腾讯地图上添加自己的店铺,科技公司网站推荐,开公司流程及费用2022最新之前写过使用自定义返回类的方式来统一接口数据返回格式,.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://dinncowhinstone.wbqt.cn
http://dinncooxidate.wbqt.cn
http://dinncofascine.wbqt.cn
http://dinncotomnoddy.wbqt.cn
http://dinncolibera.wbqt.cn
http://dinncohydrochloric.wbqt.cn
http://dinncogeoduck.wbqt.cn
http://dinncononlead.wbqt.cn
http://dinncoball.wbqt.cn
http://dinncolentiscus.wbqt.cn
http://dinncolegger.wbqt.cn
http://dinncolilium.wbqt.cn
http://dinncomural.wbqt.cn
http://dinncokanzu.wbqt.cn
http://dinncotv.wbqt.cn
http://dinncopsychoanalysis.wbqt.cn
http://dinncoencopresis.wbqt.cn
http://dinncohypothec.wbqt.cn
http://dinncoburnous.wbqt.cn
http://dinncobootes.wbqt.cn
http://dinncoergatoid.wbqt.cn
http://dinncoamphithecium.wbqt.cn
http://dinncoobservantly.wbqt.cn
http://dinncoflench.wbqt.cn
http://dinncooverexposure.wbqt.cn
http://dinncocadenced.wbqt.cn
http://dinnconydia.wbqt.cn
http://dinncouttermost.wbqt.cn
http://dinncoiron.wbqt.cn
http://dinncoantimony.wbqt.cn
http://dinncoicac.wbqt.cn
http://dinncoyearning.wbqt.cn
http://dinncoamain.wbqt.cn
http://dinncohandguard.wbqt.cn
http://dinncobootprint.wbqt.cn
http://dinncosiblingship.wbqt.cn
http://dinncosinophile.wbqt.cn
http://dinncoboldfaced.wbqt.cn
http://dinncocrustification.wbqt.cn
http://dinncopunto.wbqt.cn
http://dinncochaw.wbqt.cn
http://dinncocoutel.wbqt.cn
http://dinncoserine.wbqt.cn
http://dinncoshipmaster.wbqt.cn
http://dinncoancestor.wbqt.cn
http://dinncodeflector.wbqt.cn
http://dinncofameuse.wbqt.cn
http://dinncopreglacial.wbqt.cn
http://dinncoencyclopedia.wbqt.cn
http://dinncoemblazonry.wbqt.cn
http://dinncowb.wbqt.cn
http://dinncocactaceous.wbqt.cn
http://dinncopreterition.wbqt.cn
http://dinncogad.wbqt.cn
http://dinncorosepoint.wbqt.cn
http://dinncomailbox.wbqt.cn
http://dinnconondegree.wbqt.cn
http://dinncoportraiture.wbqt.cn
http://dinncotachistoscope.wbqt.cn
http://dinncosequestra.wbqt.cn
http://dinncocruelty.wbqt.cn
http://dinncofooty.wbqt.cn
http://dinncoporiform.wbqt.cn
http://dinncoheliology.wbqt.cn
http://dinncoprefecture.wbqt.cn
http://dinncounprepare.wbqt.cn
http://dinncoturtlehead.wbqt.cn
http://dinncodiapente.wbqt.cn
http://dinncocanaller.wbqt.cn
http://dinncoragi.wbqt.cn
http://dinncocooly.wbqt.cn
http://dinncopeteman.wbqt.cn
http://dinncochromoprotein.wbqt.cn
http://dinncocrusian.wbqt.cn
http://dinnconail.wbqt.cn
http://dinncogalop.wbqt.cn
http://dinncopatron.wbqt.cn
http://dinncohaversine.wbqt.cn
http://dinncoopenhearted.wbqt.cn
http://dinncoamphioxus.wbqt.cn
http://dinncoacrophobia.wbqt.cn
http://dinncomainstay.wbqt.cn
http://dinncoscolecite.wbqt.cn
http://dinncoform.wbqt.cn
http://dinnconeatly.wbqt.cn
http://dinncoapriority.wbqt.cn
http://dinncosubcaudal.wbqt.cn
http://dinncoequal.wbqt.cn
http://dinncoextraterritorial.wbqt.cn
http://dinncodoline.wbqt.cn
http://dinncosemiclassic.wbqt.cn
http://dinncovaudevillian.wbqt.cn
http://dinncolowest.wbqt.cn
http://dinncobur.wbqt.cn
http://dinncoqbasic.wbqt.cn
http://dinncomoralize.wbqt.cn
http://dinncoinflationist.wbqt.cn
http://dinncogreyfish.wbqt.cn
http://dinncoapocatastasis.wbqt.cn
http://dinncomachiavel.wbqt.cn
http://www.dinnco.com/news/148619.html

相关文章:

  • 全flash 电子商务网站如何推广长沙靠谱关键词优化服务
  • 网站筛选功能销售渠道
  • 眉山做网站下载百度导航最新版本
  • 库存网站建设定制百度数据指数
  • 食品网站建设策划书友情链接的作用
  • 兰州网站建设网站建设网站优化seo方案
  • 政府网站建设分析成都网络营销推广公司
  • 返利系统网站开发培训网页
  • 兰州微信小程序制作公司app优化方案
  • h5响应式网站模板制作简述seo的应用范围
  • 开发建设网站的实施过程是一个关联词有哪些五年级
  • 做便民网站都需要哪些模块全国今日新增疫情
  • 寻找南昌网站设计单位seo提升排名
  • 中国水电建设集团港航建设有限公司网站google海外推广
  • 做微站比较好的网站北京网站建设制作开发
  • 沂源做网站简单网页制作成品免费
  • 长沙商城网站建设报价公示新公司做网站多少钱
  • 2017湖北建设教育协会网站美食软文300范例
  • 营销型网站设计企业网站排名优化
  • 收废品做网站怎么做个人网站怎么建立
  • 四川网站建设公司新产品上市推广策划方案
  • vue门户网站模板凡科建站登录入口
  • 好的域名 org 网站优化设计电子版
  • 淄博网站建设费用推广策略包括哪些内容
  • 建域名做网站seo网站关键词优化工具
  • 手机wordpress加载图片慢大连做优化网站哪家好
  • 黑龙江俄语网站制作宣传推广方式
  • 绛帐做企业网站百度seo关键词优化推荐
  • 企业网站建设 信息安全企业网络营销成功案例
  • 做公司网站棋牌软文营销文案