猿问

使用ASP.NET路由来提供静态文件

可以使用ASP.Net路由(不是MVC)来提供静态文件吗?


说我要路线


http://domain.tld/static/picture.jpg


http://domain.tld/a/b/c/picture.jpg

而且我想动态地执行它,因为重写的URL是动态计算的。我不能一劳永逸地建立静态路由。


无论如何,我可以这样创建一条路线:


routes.Add(

  "StaticRoute", new Route("static/{file}", new FileRouteHandler())

);

在该FileRouteHandler.ProcessRequest方法中,我可以重写从/static/picture.jpg到的路径/a/b/c/picture.jpg。然后,我想为静态文件创建一个处理程序。ASP.NET StaticFileHandler为此使用。不幸的是,该类是内部的。我试图使用反射创建处理程序,它实际上可以工作:


Assembly assembly = Assembly.GetAssembly(typeof(IHttpHandler));

Type staticFileHandlerType = assembly.GetType("System.Web.StaticFileHandler");

ConstructorInfo constructorInfo = staticFileHandlerType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);

return (IHttpHandler) constructorInfo.Invoke(null);

但是使用内部类型似乎不是正确的解决方案。另一个选择是实现自己的实现StaticFileHandler,但是正确地做到这一点(支持诸如范围和etags之类的HTTP东西)并非易事。


如何在ASP.NET中处理静态文件的路由?


回首忆惘然
浏览 601回答 3
3回答

繁星coding

在研究了这个问题几个小时之后,我发现仅添加忽略规则将为您的静态文件提供服务。在RegisterRoutes(RouteCollection路由)中,添加以下忽略规则:routes.IgnoreRoute("{file}.js");routes.IgnoreRoute("{file}.html");

翻阅古今

为什么不使用IIS来做到这一点?您可以创建重定向规则,以将从第一个路由到第二个路由的所有请求指向该请求,甚至可以将其到达您的应用程序。因此,这将是重定向请求的更快方法。假设您拥有IIS7 +,则可以执行以下操作:<rule name="Redirect Static Images" stopProcessing="true">&nbsp; <match url="^static/?(.*)$" />&nbsp; <action type="Redirect" url="/a/b/c/{R:1}" redirectType="Permanent" /></rule>或者,如果您不需要重定向,如@ ni5ni6所示:<rule name="Rewrite Static Images" stopProcessing="true">&nbsp; <match url="^static/?(.*)$" />&nbsp; <action type="Rewrite" url="/a/b/c/{R:1}" /></rule>为@RyanDawkins编辑2015-06-17:而且,如果您想知道重写规则的去向,这里是其在web.config文件中位置的映射。<?xml version="1.0" encoding="utf-8" ?><configuration>&nbsp; <system.webServer>&nbsp; &nbsp; <rewrite>&nbsp; &nbsp; &nbsp; <rules>&nbsp; &nbsp; &nbsp; &nbsp; <!-- rules go below -->&nbsp; &nbsp; &nbsp; &nbsp; <rule name="Redirect Static Images" stopProcessing="true">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <match url="^static/?(.*)$" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action type="Redirect" url="/a/b/c/{R:1}" redirectType="Permanent" />&nbsp; &nbsp; &nbsp; &nbsp; </rule>&nbsp; &nbsp; &nbsp; </rules>&nbsp; &nbsp; </rewrite>&nbsp; </system.webServer></configuration>

莫回无

我有类似的问题。我最终使用了HttpContext.RewritePath:public class MyApplication : HttpApplication{&nbsp; &nbsp; private readonly Regex r = new Regex("^/static/(.*)$", RegexOptions.IgnoreCase);&nbsp; &nbsp; public override void Init()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; BeginRequest += OnBeginRequest;&nbsp; &nbsp; }&nbsp; &nbsp; protected void OnBeginRequest(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var match = r.Match(Request.Url.AbsolutePath);&nbsp; &nbsp; &nbsp; &nbsp; if (match.Success)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var fileName = match.Groups[1].Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Context.RewritePath(string.Format("/a/b/c/{0}", fileName));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答