.net core 实现默认图片

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

.net core 实现默认图片

 

Startup 文件

1

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

新建类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

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 { getset; }

 

        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图片  即可

 

https://www.cnblogs.com/lyl6796910/p/7660076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值