【问题标题】:Controller for serving images doesn't work with extensions用于提供图像的控制器不适用于扩展
【发布时间】:2015-02-12 16:00:33
【问题描述】:

我想使用 ImageController 来提供我的图像:

public class ImageController : Controller
{
    // GET: Image
    public ActionResult Render(string fileName)
    {
        byte[] image = System.IO.File.ReadAllBytes(/*some path*/);
        return this.Image(image, "image/png");
    }
}

我添加了路线:

routes.MapRoute(
    name: "Images",
    url: "images/{action}/{fileName}",
    defaults: new { controller = "Image", action = "Render", fileName = UrlParameter.Optional }
);

所以这个图像路径有效: http://xxx.xxx/images/render/x

但带有此扩展名的路径不会: http://xxx.xxx/images/render/x.png

出现“HTTP 错误 404.0 - 未找到”错误。

我应该怎么做才能启用文件扩展名?

【问题讨论】:

    标签: asp.net-mvc routing asp.net-mvc-routing


    【解决方案1】:

    尝试以下步骤:

    1. 在 RouteConfig.cs 中启用自定义文件路由

      routes.RouteExistingFiles = true;
    2. 在您的 web.config 中添加以下处理程序

    <system.webServer>
      <handlers>
        <add name="ProtectedImages" path="images/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler"   preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      相关资源
      最近更新 更多