繁体   English   中英

使用Asp.Net和IIS中的路由实际物理文件的虚拟路径

[英]Virtual paths to actual physical files using Routing in Asp.Net and IIS

我使用Asp.Net 4 C#with Routing。

我使用Routing将URL重新路由到实际的物理文件。 我的代码与代码完全相同。

我的目标是使用自定义路径创建图像src属性,但是在不同文件夹中具有图像的物理路径。

示例输出路由:

<img alt="" src="/cdn/img/cms/e71e75ef-4906-4d04-8da5-bd459c7b9205/titolo-image.jpg"/>

物理路径:

/cdn/img/cms/images/large/e71e75ef-4906-4d04-8da5-bd459c7b9205.jpg

在使用Visual Studio 2010在本地环境中进行测试时,CASSINI上的一切都很有效,但是一旦网站在生产环境中运行,使用IIS 7.5就无法找到图像src结果。

我想IIS不像.Net页面一样处理静态文件.jpg的请求。

我想知道解决这种情况有哪些选择。

这里我用来扩展图像路由的代码..

使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用System.Web; 使用System.Web.Routing;

namespace WebProject.Cms.BusinessLogics.SEO.Routing
{
    /// <summary>
    /// Handler Custom Class for Asp.Net Routing. Routing URL's (Virtual paths) to actual physical files.
    /// <remarks>Resource at: http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/</remarks>
    /// </summary>
    public class ImageRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            string filename = requestContext.RouteData.Values["RowGuid"] as string;
            string size = requestContext.RouteData.Values["Size"] as string;
            string imageType = requestContext.RouteData.Values["ImageType"] as string;

            bool hasFileName = !string.IsNullOrEmpty(filename);
            bool hasSize =
                size == "large" ||
                size == "medium" ||
                size == "small";
            bool hasImageType =
                imageType == "jpg" ||
                imageType == "bmp" ||
                imageType == "gif" ||
                imageType == "png";

            if (!hasFileName || !hasSize || !hasImageType)
            {
                requestContext.HttpContext.Response.Clear();
                requestContext.HttpContext.Response.StatusCode = 404;
                requestContext.HttpContext.Response.End();
            }
            else
            {
                requestContext.HttpContext.Response.Clear();
                requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());

                // Find physical path to image.
                string filepath = requestContext.HttpContext.Server.MapPath("~/Cdn/Cms/Images/" + size + "/" + filename + "." + imageType);

                requestContext.HttpContext.Response.WriteFile(filepath);
                requestContext.HttpContext.Response.End();
            }
            return null;
        }

        private static string GetContentType(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".bmp": return "Image/bmp";
                case ".gif": return "Image/gif";
                case ".jpg": return "Image/jpeg";
                case ".png": return "Image/png";
                default: break;
            }
            return "";
        }
    }
}

您怀疑是对的,您需要确保在web.config中定义IIS应该通过JPG请求路由到新的自定义处理程序。

本文看起来是IIS7中集成管道的一个很好的介绍指南,以及如何根据您的需求进行自定义。 这只是在web.config中注册你的处理程序并设置它以便所有* .jpg路径都转到你的处理程序的情况。

根据PirateKitten的建议,我能够解决我的问题,这里是我web.config中的工作代码:

   <system.webServer>
...

        <handlers>
            <add name="Cms-ImageRouteHandler" path="*.jpg" verb="*" type="WebProject.Cms.BusinessLogics.SEO.Routing.ImageRouteHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
        </handlers>
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM