zoukankan      html  css  js  c++  java
  • .net core 实现默认图片

    web 上 如果图片不存在 一般是打xx  这时候 一般都是会设置默认的图片 代替   现在用中间件的方式实现统一设置   一次设置 全部作用 

    .net core 实现默认图片

    Startup 文件

     app.UseDefaultImage(defaultImagePath: Configuration.GetSection("defaultImagePath").Value);
    

      新建类

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Http;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace conan.Saas.Framework.Middlewares
    {
        public class DefaultImageMiddleware
        {
            private readonly RequestDelegate _next;
    
            public static string DefaultImagePath { get; set; }
    
            public DefaultImageMiddleware(RequestDelegate next)
            {
                this._next = next;
            }
    
            public async Task Invoke(HttpContext context)
            {
                await _next(context);
                if (context.Response.StatusCode == 404)
                {
                    var contentType = context.Request.Headers["accept"].ToString().ToLower();
                    if (contentType.StartsWith("image"))
                    {
                        await SetDefaultImage(context);
                    }
                }
            }
    
            private async Task SetDefaultImage(HttpContext context)
            {
                try
                {
                    string path = Path.Combine(Directory.GetCurrentDirectory(), DefaultImagePath);
    
                    FileStream fs = File.OpenRead(path);
                    byte[] bytes = new byte[fs.Length];
                    await fs.ReadAsync(bytes, 0, bytes.Length);
                    //this header is use for browser cache, format like: "Mon, 15 May 2017 07:03:37 GMT". 
                    //context.Response.Headers.Append("Last-Modified", $"{File.GetLastWriteTimeUtc(path).ToString("ddd, dd MMM yyyy HH:mm:ss")} GMT");
    
                    await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
                }
                catch (Exception ex)
                {
                    await context.Response.WriteAsync(ex.Message);
                }
            }
        }
    
        public static class DefaultImageMiddlewareExtensions
        {
            public static IApplicationBuilder UseDefaultImage(this IApplicationBuilder app, string defaultImagePath)
            {
                DefaultImageMiddleware.DefaultImagePath = defaultImagePath;
    
                return app.UseMiddleware<DefaultImageMiddleware>();
            }
        }
    }
    

      

    appsettings.json 添加路径

     "defaultImagePath": "wwwroot\DefaultImage.png",

     最后 在 wwwroot   放张  DefaultImage.png图片  即可

  • 相关阅读:
    ID:未找到命令-BASH:TTY:未找到命令
    连接/登录/访问 FTP超时、时间长,一条配置解决
    PlantUML integration plugin IDEA
    使用sc.exe delete 服务名 删除Windows下的【安装错误的、不能使用的】服务
    Eclipse JDT Icons(Java Development Tools 图标)
    Seata分布式事务——no available server to connect解决
    Slf4j Logger logger.info的使用
    SonarQube网页端登录失败的解决
    SpringBoot属性加载顺序
    W3School-SQL测验记录
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/7660076.html
Copyright © 2011-2022 走看看