ImageResizer.net和Owin UseStaticFiles

时间:2015-03-18 10:52:16

标签: c# owin imageresizer static-files

我正在使用ImageResizer并使用Owin StaticFiles 像:

app.UseStaticFiles(new StaticFileOptions
        {
            RequestPath = new PathString("/photos/user"),
            FileSystem = new PhysicalFileSystem(@".\uploads\photos"),
           // EnableDirectoryBrowsing = true,
        });

的web.config

<system.webServer>
    <handlers>
      ... 

        <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" />   
      ....
</handlers>

ImageResizing配置得很好我可以通过在webconfig中注释Owin处理程序来确认它

然而,当我同时使用它们时,我似乎无法使用ImageResizer

喜欢

http://localhost:7805/photos/user/admin.png?w=120

图片未调整为指定宽度

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并找到了适合我的解决方案。如果对你来说太晚了,也许它可以帮助别人。

我为我的图片创建了以下中间件:

app.Use(async (ctx, next) =>
{
  var endings = new[] { ".jpg", ".jpeg", ".png", ".gif" };
  if (endings.Any(e => ctx.Request.Path.Value.EndsWith(e)) &&
      ctx.Request.QueryString.HasValue &&
      File.Exists(HostingEnvironment.MapPath(ctx.Request.Path.Value)))
  {
    var inputStream = File.OpenRead(HostingEnvironment.MapPath(ctx.Request.Path.Value));

    var outputStream = ctx.Response.Body;

    var job = new ImageJob(inputStream, outputStream, new Instructions(ctx.Request.QueryString.Value));

    var cfg = new ImageResizer.Configuration.Config();
      cfg.Build(job);
  }
  else
  {
      await next();
  }
});

我把它放在我的Startup中的“UseStaticFiles”之前。当然,您可以根据自己的喜好/需求更改配置。

相关问题