【问题标题】:Virtual paths to actual physical files using Routing in Asp.Net and IIS在 Asp.Net 和 IIS 中使用路由到实际物理文件的虚拟路径
【发布时间】:2011-12-10 00:31:59
【问题描述】:

我使用带有路由的 Asp.Net 4 C#。

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

我的目标是使用自定义路由创建图像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 "";
        }
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc iis-7 routing


    【解决方案1】:

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

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

    【讨论】:

    • 是 smt 我只能在 web.config 中做,还是必须直接在 IIS 7 中修改 smt?感谢您的宝贵时间。
    • IIS 7 通常将所有站点配置存储在 web.config 中(如果需要,您可以将其存储在其他位置),因此您应该可以直接编辑它。
    • 感谢 PirateKitten 我能够让它工作.. 但我的问题在这里有一个小问题,也许你知道答案非常感谢stackoverflow.com/questions/7810435/…
    【解决方案2】:

    按照 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>
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多