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

网站编辑做的准备外贸建站公司排名

网站编辑做的准备,外贸建站公司排名,个人建设什么网站,广西网站建设哪里有问题在业务开发中#xff0c;对Web API的返回格式有一定要求#xff0c;需要是定制化的Json结构#xff0c;用于前端统一处理#xff1a;{Status : 0,Message: ,Info : xxx }Status表示响应的状态码#xff0c;0为成功#xff1b;Message表示错误消息#x… 问题在业务开发中对Web API的返回格式有一定要求需要是定制化的Json结构用于前端统一处理{Status : 0,Message: ,Info : xxx } Status表示响应的状态码0为成功Message表示错误消息Status不为0时返回Info表示API返回的实际数据Json格式简单实现当然你可以定义一个数据结构作为每个API的返回值public class ResponseDataT {public int Status { get; set; }  0;public string Message { get; set; }public T Info { get; set; }public ResponseData(T obj){Info  obj;} }[HttpGet] public ResponseDataIEnumerableWeatherForecast Get() {var rng  new Random();var data  Enumerable.Range(1, 5).Select(index  new WeatherForecast{Date  DateTime.Now.AddDays(index),TemperatureC  rng.Next(-20, 55),Summary  Summaries[rng.Next(Summaries.Length)]}).ToArray();return new ResponseDataIEnumerableWeatherForecast(data); } 但是如果这样实现每一个API方法都必须修改实例化一个ResponseData对象返回。如果以后业务修改要移除这个自定义结构又是件麻烦事。有没有一劳永逸、并且更加优雅的实现方式呢自定义响应内容既然这个Json结构是在原有的返回数据外围再包了一层那么我们直接获取Web API的原始Response.Body然后格式化成新的JSon在赋值给Response.Body不就可以了 但是实际验证时发现在.NET 5下已经无法改写无任何数据返回。示例代码如下app.Use(async (context, next)  {var newContent  string.Empty;using (var newBody  new MemoryStream()){context.Response.Body  newBody;await next();context.Response.Body  new MemoryStream();newBody.Seek(0, SeekOrigin.Begin);newContent  new StreamReader(newBody).ReadToEnd();newContent  , World!;await context.Response.WriteAsync(newContent);} }); 难道这条路走不通IHttpResponseBodyFeature在aspnetcore的源代码中找到了ResponseCompressionMiddlewarehttps://github.com/dotnet/aspnetcore/blob/main/src/Middleware/ResponseCompression/src/ResponseCompressionMiddleware.cs。它是用来处理响应压缩的中间件也就是说对响应做了处理看看它的实现方式public async Task Invoke(HttpContext context) {if (!_provider.CheckRequestAcceptsCompression(context)){await _next(context);return;}var originalBodyFeature  context.Features.GetIHttpResponseBodyFeature();var originalCompressionFeature  context.Features.GetIHttpsCompressionFeature();Debug.Assert(originalBodyFeature ! null);var compressionBody  new ResponseCompressionBody(context, _provider, originalBodyFeature);context.Features.SetIHttpResponseBodyFeature(compressionBody);context.Features.SetIHttpsCompressionFeature(compressionBody);try{await _next(context);await compressionBody.FinishCompressionAsync();}finally{context.Features.Set(originalBodyFeature);context.Features.Set(originalCompressionFeature);} } 它将IHttpResponseBodyFeature进行了替换context.Features.SetIHttpResponseBodyFeature(compressionBody); 那IHttpResponseBodyFeature到底是个什么玩意“ASP.NET Core 中的请求功能”https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/request-features?viewaspnetcore-5.0作出了相应的解释 ASP.NET Core 在 Microsoft.AspNetCore.Http.Features 中定义了许多常见的 HTTP 功能接口各种服务器和中间件共享这些接口来标识其支持的功能。服务器和中间件还可以提供自己的具有附加功能的接口。 ResponseCustomBody那我们就依葫芦画瓢实现我们的ResponseCustomBody:public class ResponseCustomBody : Stream, IHttpResponseBodyFeature {private readonly HttpContext _context;private readonly IHttpResponseBodyFeature _innerBodyFeature;private readonly Stream _innerStream;public ResponseCustomBody(HttpContext context,IHttpResponseBodyFeature innerBodyFeature){_context  context;_innerBodyFeature  innerBodyFeature;_innerStream  innerBodyFeature.Stream;} public Stream Stream  this;public PipeWriter Writer  throw new NotImplementedException();public override bool CanRead  false;public override bool CanSeek  false;public override bool CanWrite  _innerStream.CanWrite;public override long Length  throw new NotImplementedException();public override long Position { get  throw new NotImplementedException(); set  throw new NotImplementedException(); }public async Task CompleteAsync(){await _innerBodyFeature.CompleteAsync();}public void DisableBuffering(){_innerBodyFeature.DisableBuffering();}public Task SendFileAsync(string path, long offset, long? count, CancellationToken cancellationToken  default){return _innerBodyFeature.SendFileAsync(path, offset, count, cancellationToken);}public Task StartAsync(CancellationToken cancellationToken  default){return _innerBodyFeature.StartAsync(cancellationToken);}public override void Flush(){_innerStream.Flush();}public override int Read(byte[] buffer, int offset, int count){throw new NotImplementedException();}public override long Seek(long offset, SeekOrigin origin){throw new NotImplementedException();}public override void SetLength(long value){throw new NotImplementedException();}public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken){var json  System.Text.Encoding.UTF8.GetString(buffer).TrimEnd(\0);json  {\Status\:0, \Info\:  json   };buffer  System.Text.Encoding.UTF8.GetBytes(json);count  buffer.Length;await _innerStream.WriteAsync(buffer, offset, count, cancellationToken);}public override void Write(byte[] buffer, int offset, int count){throw new NotImplementedException();} } 关键代码就是下面这段我们取出原始响应内容格式化后再写入public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) {var json  System.Text.Encoding.UTF8.GetString(buffer).TrimEnd(\0);json  {\Status\:0, \Info\:  json   };buffer  System.Text.Encoding.UTF8.GetBytes(json);count  buffer.Length;await _innerStream.WriteAsync(buffer, offset, count, cancellationToken); } 最后我们再定义一个中间件使用ResponseCustomBody替换IHttpResponseBodyFeaturepublic class ResponseCustomMiddleware : IMiddleware {public async Task InvokeAsync(HttpContext context, RequestDelegate next){var originalBodyFeature  context.Features.GetIHttpResponseBodyFeature();var customBody  new ResponseCustomBody(context, originalBodyFeature);context.Features.SetIHttpResponseBodyFeature(customBody);try{await next(context);}finally{context.Features.Set(originalBodyFeature);}}  } 运行效果也能满足我们的要求 结论在本文中我们利用了ASP.NET Core的功能接口实现了自定义格式响应。你也可以尝试使用功能接口实现一些有用的功能。如果你觉得这篇文章对你有所启发请关注我的个人公众号”My IO“记住我
http://www.yutouwan.com/news/237009/

相关文章:

  • 长沙网站制作案例廊坊网站建设招聘
  • 外贸企业网站模版游戏代理0加盟费
  • 给视频做特效的网站seo工具是什么意思
  • 微信订阅号做微网站江苏中兴建设有限公司网站
  • 网站模版idc公司网站建设升上去
  • 在线相册jsp网站开发与设计宝安中心是富人区吗
  • 50个优秀网站保山市住房和城乡建设局门户网站
  • 成都手机网站建如今做啥网站能致富
  • 网站流量少加盟创业
  • 网站公司未来计划ppt怎么做wordpress编辑不了
  • nodejs网站开发做网站坚持原创文章有什么好处
  • 网站开发 明细网站的专题模板制作软件
  • 百度建设网站的目的安徽省建设厅网站工程师查询
  • php网站收录中关村在线官网首页
  • 设计网站源码衡水做企业网站的公司
  • 网站建设信(信科网络)敦化市住房和城乡建设局网站
  • 网站改版的意义房地产销售工作总结
  • 嘉兴市南湖区建设街道网站做电影网站多少钱
  • 什么网站可以自己做房子设计图建筑网片多少钱一平方
  • 网站备案期限免费设计房屋装修软件
  • 网站网页设计项目计划书西宁做腋臭哪里北大DE网站
  • 重庆百度网站快速排名应该如何做营销型网站
  • 网站无法显示网页内容aitt网站建设中
  • 律师个人网站建设SEO网站建设全方位部署
  • 个人建设网站成本wordpress for linux
  • 中小企业网站建设济南兴田德润厉害吗机械加工网瓦房北方机床附件厂
  • 域名服务网站建设科技公司wordpress网站克隆
  • 怎么做网站平台教程可以建设彩票网站吗
  • 国外电子政务j建设与我国电子政务网站建设对比俄语网站建站
  • 网站建设找伟杨科技ios企业开发者账号