【问题标题】:ASP.NET Core middleware return imageASP.NET Core 中间件返回图片
【发布时间】:2021-03-13 10:13:45
【问题描述】:

我有一个 asp.net 核心中间件,它应该记录一些请求内容,然后返回一个图像。

在中间件响应中,我喜欢将图像作为字节 [] 返回。我玩弄了 context.Reponse.WriteAsync() 对象,但没有找到任何返回图像的解决方案。

  public class TrackerMiddleware
    {
        private readonly IClientInformationManager _clientInformationManager;
        private readonly RequestDelegate _next;

        public TrackerMiddleware(RequestDelegate next, IClientInformationManager clientInformationManager)
        {
            _next = next;
            _clientInformationManager = clientInformationManager;
        }

        public async Task Invoke(HttpContext context)
        {
            var clientInfo = new ClientInformationLogDTO();
            clientInfo.AgentContext = context.Request.Headers["User-Agent"];

          
            await _clientInformationManager.CreateLogAsync(clientInfo);

            //TODO: Return image here
            await context.Response.WriteAsync("");

            //await _next(context);
        }
    }

如何修改我的中间件以返回图像?

【问题讨论】:

    标签: response middleware


    【解决方案1】:
    await context.Response.BodyWriter.WriteAsync(new byte[]{ 1 });
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
    猜你喜欢
    • 2019-03-08
    • 2021-07-13
    • 2017-07-22
    • 2017-04-09
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多