【问题标题】:C# Adding Image to Owin Context.ResponseC# 将图像添加到 Owin Context.Response
【发布时间】:2015-11-05 04:56:48
【问题描述】:

我的 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# html response owin middleware


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 2015-03-17
      • 2014-02-20
      • 2012-08-27
      • 2015-01-23
      相关资源
      最近更新 更多