简体   繁体   中英

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

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 "";
        }
    }
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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