使用Asp.Net和IIS中的路由到实际物理文件的虚拟路径 [英] Virtual paths to actual physical files using Routing in Asp.Net and IIS
问题描述
我使用Asp.Net 4 C#进行路由。
I use Asp.Net 4 C# with Routing.
我使用路由将URL重新路由到实际的物理文件。我的代码与这一个。
I use Routing to re-route URL's to actual physical files. My code is pretty identical to this one.
我的目标是创建图像 src
属性使用自定义路径,但将图像的物理路径放在其他文件夹中。
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.
示例输出路径:
<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
找不到结果。
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.
我想IIS不会以与.Net页面相同的方式处理对静态文件.jpg的请求。
我想知道我有什么解决方案。
I would like to know what are my options to solve this situation.
我用来扩展图像路由的代码。
Here the code I use to extend the Routing for images..
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Web;
使用System.Web.Routing;
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 "";
}
}
}
推荐答案
您怀疑是正确的,您需要确保已在web.config中定义了IIS应该通过JPG请求路由到新的自定义处理程序。
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.
本文如下:它是IIS7中集成管道的良好介绍指南,以及如何根据需要对其进行自定义。这只是在web.config中注册您的处理程序并将其设置为所有* .jpg路径都转到您的处理程序的情况。
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.
这篇关于使用Asp.Net和IIS中的路由到实际物理文件的虚拟路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!