我不想将HTTP请求路由到操作,而是将其路由到文件。
重要:i确实有一个使用IIS7.0URL重写模块的工作解决方案,但是对于在家里进行调试(没有IIS7.0),我不能使用重写。
特殊情况
我希望将包含/images/的任何URL指向~/images/文件夹。
示例:
http://wowreforge.com/images/a.png -> /images/a.png
http://wowreforge.com/Emerald Dream/Jizi/images/a.png -> /images/a.png
http://wowreforge.com/US/Emerald Dream/Jizi/images/a.png -> /images/a.png
http://wowreforge.com/characters/view/images/a.png -> /images/a.png这个问题源于一个事实,即页面"view_character.aspx“可以从多个URL到达:
http://wowreforge.com/?name=jizi&reaml=Emerald Dream
http://wowreforge.com/US/Emerald Dream/JiziContext IIS7.0(集成模式),ASP.NET MVC 2.0
额外学分问题
在这种情况下使用MVC路由(而不是URL rewriting?
发布于 2011-01-17 18:43:27
您可能应该重写到图片的链接到。
<img src="<%= ResolveUrl("~/images/a.png") %>" />这样你就不需要让你的路线来处理图像了。
更新将如何通过路由将此条目添加到RouteTable中
routes.Add("images", new Route("{*path}", null,
new RouteValueDictionary(new { path = ".*/images/.*"}),
new ImageRouteHandler()));现在您需要创建一个ImageRouteHandler和一个ImageHandler
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
//you'll need to figure out how to get the physical path
return new ImageHandler(/* get physical path */);
}
}
public class ImageHandler : IHttpHandler
{
public string PhysicalPath { get; set; }
public ImageHandler(string physicalPath)
{
PhysicalPath = physicalPath;
}
public void ProcessRequest(HttpContext context)
{
context.Response.TransmitFile(PhysicalPath);
}
public bool IsReusable
{
get { return true; }
}
}这也不做任何缓存。您可以在Reflector中签出System.Web.StaticFileHandler,用于处理Asp.Net应用程序的静态文件,以获得更完整的实现。
https://stackoverflow.com/questions/4716574
复制相似问题