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

一笑奈何 提交于 2019-12-11 15:27:56

问题


I use Asp.Net 4 C# with Routing.

I use Routing to re-route URL's to actual physical files. My code is pretty identical to this one.

My aim is to have images src attribute created using a custom Route but to have the physical path for the image in a different folder.

Example output Route:

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

The phisical path:

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

Everything works great on CASSINI when testing on a local enviroment with Visual Studio 2010, but as soon as the website runs in production enviroment wich use IIS 7.5 the image src result not found.

I suppose IIS is not handling the request for a static file .jpg in the same way of a .Net Page.

I would like to know what are my options to solve this situation.

Here the code I use to extend the Routing for images..

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using 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 "";
        }
    }
}

回答1:


You suspect right, you need to make sure you have defined in your web.config that IIS should be routing through JPG requests to your new custom handler.

This article looks like it's a good introduction guide to the Integrated Pipeline in IIS7 and how you can customize it to your needs. It will just be a case of registering yoour handler in the web.config and setting it so that all *.jpg paths go to your handler.




回答2:


Following the advice from PirateKitten, I was able to solve my problem, here the working code in my 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>
...


来源:https://stackoverflow.com/questions/7806414/virtual-paths-to-actual-physical-files-using-routing-in-asp-net-and-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!