C#将图像添加到Owin上下文.响应

本文关键字:Owin 上下文 响应 添加 图像 | 更新日期: 2023-09-27 18:20:40

我的C#MVC项目在Owin中间件中有授权逻辑,我想在用户未被授权时向浏览器显示错误消息。我的方法是在Owin中间件中使用Response.WriteAsync()方法来显示页面。Response.WriteAsync()的内容来自单独的"error.html"页面,我使用File.ReadAllText()读取内容。

我有"error.html"页面:

...
<body>
     <image src = "errorImage.gif">
     <p>Not Authorized</p>
</body>
...

Owin中间件:

public class middleware : OwinMiddleware
{
    public async override Task Invoke(IOwinContext context){
         var errorPage = File.ReadAllText("error.html"); //Here I am reading the html page
         if(not_authorized){
              context.Response.WriteAsync(errorPage); // Displaying the page
         }
         await Next.Invoke(context);
    }
}

这种方法的问题是,我无法显示"error.html"页面中关联的图像,因为图像位于服务器中。在这种情况下,有没有办法显示带有图像的错误页面?

C#将图像添加到Owin上下文.响应

将图像作为base64字符串传递,然后在src标记中引用base64字符串中的图像:

<img src="data:[imgType ie: image/gif];base64,[base64StringYouPassedDown]" />