【问题标题】:Preprocess png images before sending response在发送响应之前预处理 png 图像
【发布时间】:2017-12-14 06:40:05
【问题描述】:

假设我们有 MVC 应用程序和图像位于:/Images/image1.png

我们有匹配这个特定路线的行动

[Route("Content/{filename}")]
public ActionResult GetImage(string filename)
{
   // Process image (add watermark etc..)
}

目标是在将 png 文件发送到浏览器之前对其进行处理。

当我们尝试访问此操作时出现问题:

/Content/image1.png

它返回 404,因为 .png 扩展名是作为静态内容处理的,所以它认为我正在尝试从该位置加载图像。另一方面,如果我们在文件名之后添加一个斜杠: /Content/image1.png/ 它确实可以正常工作

问题是我们如何让 iis 忽略 png 并将其作为路由而不是静态内容处理

我在 web.config 中尝试了这个但没有运气:

<system.webServer>
    <staticContent>
      <remove fileExtension=".png" />

    </staticContent>
  </system.webServer>

【问题讨论】:

    标签: asp.net asp.net-mvc web-config


    【解决方案1】:

    好的。找到了解决方案。你需要添加

    <handlers>
      <add name="PngFileHandler" path="*.png" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    

    找到here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      相关资源
      最近更新 更多