即使文件不存在,如何访问处理程序?

时间:2018-01-08 17:08:01

标签: c# asp.net asp.net-mvc asp.net-mvc-5 ihttphandler

我正在使用 - asp.net mvc。

targetFramework="4.6.1"

我创建了一个处理程序,需要检查图像是否存在于本地文件夹中(如果存在) - 返回图像,如果不存在 - 它将转到服务器URL图像路径并从那里下载到本地文件夹。而不是返回图像。

MyHandler:HttpImageHandler.cs

using SearchContentPortal.Core;
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;

namespace SearchContentPortal.Handlers
{
    public class HttpImageHandler : IHttpHandler
    {

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ClearContent();
            context.Response.ClearHeaders();
            var fileName = Path.GetFileName(context.Request.PhysicalPath);
            var localfilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Images\media", fileName);
            //check if file not exists in local folder,  or the last modify file wasn't today
            if (!File.Exists(localfilePath) || (File.Exists(localfilePath) && (File.GetLastWriteTime(localfilePath).ToShortDateString() != DateTime.Today.ToString("dd/MM/yyyy"))))
            { //if true - download the file to local folder
                DownloadAndSaveImage(localfilePath, fileName);
            }

            //return file stream
            Bitmap bitmap = new Bitmap(context.Server.MapPath("~/Images/media/" + fileName));

            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.WriteTo(context.Response.OutputStream);
                context.Response.ContentType = "image/png";
            }

            context.Response.Flush();
            context.Response.End();
        }

        public static void DownloadAndSaveImage(string localfilePath, string fileName)
        {
            using (WebClient client = new WebClient())
            {
                client.DownloadFile(AppSettings.ImagesBaseUrl + "/" + fileName, localfilePath);
            }
        }

    }
}

我的Web.config

  <system.webServer>
    <handlers>
      <add name="HttpImageHandler1" path="*.png" verb="*" 
      type="SearchContentPortal.Handlers.HttpImageHandler" 
      preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="HttpImageHandler2" path="*.jpg" verb="*" 
      type="SearchContentPortal.Handlers.HttpImageHandler" 
      preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

我的形象 -

 <img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast"  />

但它不起作用 - 没有进入处理程序。

如果我已经在“media”文件夹中拥有物理图像。 (如果存在) - 它输入了hadler

我不能在src中使用 - “Handlers / ImageHandler.ashx?fileName = beauty_beast.jpg”。 我的意思是不使用ashx文件(HttpImageHandler.ashx),如:

 <img src="Handlers/ImageHandler.ashx?fileName=beauty_beast.jpg" alt="beauty and the beast"/>

我想在src中看到路径 像这样:

 <img src="~/Images/media/beauty_beast.jpg" alt="beauty and the beast"/>

什么是正确的解决方案?

谢谢!

1 个答案:

答案 0 :(得分:1)

在ASP.Net Web窗体中,它可以正常工作,但在MVC中,您需要忽略此路径,不要将其视为控制器操作路径,因此:

routes.IgnoreRoute("Images/{*pathInfo}");

或一般

routes.IgnoreRoute("Images/");

ASP.NET Routing - Ignore routes for files with specific extension, regardless of directory