asp.net-mvc - 带 URL 重写的 MVC 图像处理程序

标签 asp.net-mvc image url-rewriting handler

我正在尝试构建一个处理程序,以友好的方式返回图像,例如http://mydomain.com/images/123/myimage.jpg甚至.../123/myimage

我之前在使用 .NET Forms 和 ashx 文件之前已经完成过此操作。

我现在正在使用 MVC 4(我是新手)并尝试做同样的事情。我重新使用了很多旧代码,并将 ashx 文件添加到我的项目中,并通过查询字符串来成功生成我的图像。但是,我就是无法让 URL 重写工作!

在我的旧代码中我使用:

        RouteTable.Routes.MapHttpHandlerRoute("imagestoret", "imagestore/{fileId}", "~/Images/ImageHandler.ashx");
        RouteTable.Routes.Add(new Route("imagestore/{fileId}", new PageRouteHandler("~/Images/ImageHandler.ashx")));

其中 MapHttpHandlerRoute 是在互联网上找到的自定义类,其中包含:

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        if (!string.IsNullOrEmpty(_virtualPath))
        {
            return (IHttpHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(IHttpHandler));
        }
        else
        {
            throw new InvalidOperationException("HttpHandlerRoute threw an error because the virtual path to the HttpHandler is null or empty.");
        }
    }

从那时起,我尝试将其转换为 Controller ,该 Controller 可以成功地使用查询字符串,但是,当我尝试在其中添加路由时,仍然返回 404 错误。

routes.MapRoute(
            "ImageProvider",
            "imagestore/{fileId}/",
            new { controller = "File", action = "GetFile", id = UrlParameter.Optional });

我还尝试了来自互联网的 ImageRouteHandler:

    public class ImageRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // Do stuff
    }
}

然后将以下内容添加到我的 RouteConfig.cs 中:

        routes.Add("MyImageHandler",
            new Route("imagex/{fileId}",
            new ImageRouteHandler())
        );

有人知道我错在哪里吗?

提前致谢。

最佳答案

我还没有找到完美的解决方案,但有一个折衷方案。如果没有 {controller} 方面,我根本无法让路由正常工作。所以我最终所做的是添加一条新路线:

        routes.MapRoute(
            "FileProvider",
            "{controller}/{action}/{id}/{name}",
            new { controller = "File", action = "GetFile", id = UrlParameter.Optional },
            new[] { "mynamespace.Controllers" }
        );

这允许路由到我的 Controller > FileController > GetFile 方法,例如

    public FileResult GetFile(int id, string name)
    {
        DB.UploadedFile file;

        file = DB.UploadedFile.GetFile(4, DB.UploadedFile.UploadedFileType.IMAGE, id, name);

        if (file != null && DB.UploadedFile.IsImage(file.Filename))
        {
            ImageFormat imgFormat = GetImageFormat(file.Extension);

            if (imgFormat != ImageFormat.Icon)
            {
                return File(file.FileContentsAsByteArray, file.ContentType);
            }
        }
        return null;
    }

因此,这使我能够提供如下图像:

http://mydomain.com/File/GetFile/1/DogsAndCats

代码确保 ID 和名称匹配,这样人们就不能只搜索文件数据库。

目前文件没有扩展名,这可能会导致问题,但到目前为止 - 只要设置了内容类型),图像就会正确加载。

我的代码中还有更多工作要做才能为其他文件类型提供服务,但也许这种妥协对其他人有用。

关于asp.net-mvc - 带 URL 重写的 MVC 图像处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792004/

相关文章:

c# - NLog 减慢并破坏了我的 ASP.net MVC 应用程序

c# - ASP.NET MVC Html.EnumDropDownListFor 选定值

c# - 当我的 View 模型有构造函数时发布数据不起作用

java - 如何将 Tomcat 重写阀添加到 Spring Boot 2.0 应用程序

javascript - 非 PushState 浏览器中的 JQuery Mobile 查询字符串问题

php - URL 重写 : css, js,图片未加载

asp.net-mvc - 获取用于 CDN 的 ASP.NET 包 URL?

javascript - 如何在javascript中获取当前点击的图片的数量

java - 如何在JFrame的标题栏中添加图片图标?

image - R:将 alpha 值添加到 png-image