我想访问一个静态文件,并不是让网站去根据路径去寻找静态资源文件,而是通过路由,来访问静态资源文件,
烦躁。。。搞了一下午了,首先,下面方法可以做到,但是是不可取的!
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true" />
- </system.webServer>
复制代码 有的人,叫这样写,所有访问都经过路由,不管用的。。。。
- routes.RouteExistingFiles = true;
复制代码
正确写法:
路由的定义:
- public static void RegisterRoutes(RouteCollection routes)
- {
- //routes.RouteExistingFiles = true;
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- //routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(css|js|gif|jpg)(/.*)?" });
- routes.MapRoute(
- "Read",
- "Public/{file}.{format}",
- new { controller = "Read", action = "Index" },
- new { format = @"jpg|png|bmp|gif" }
- );
- }
复制代码
web.config配置:
- <system.webServer>
- <handlers>
- <!--<add name="UrlRoutingModule-4.0" verb="GET,HEAD" path="*.jpg,*.jpeg,*.gif,*.png,*.bmp" type="System.Web.StaticFileHandler"/>-->
- <add name="file-jpg" verb="GET,HEAD" path="/public/*.jpg" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode"/>
- <add name="file-png" verb="GET,HEAD" path="/public/*.png" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode"/>
- <add name="file-bmp" verb="GET,HEAD" path="/public/*.bmp" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode"/>
- <add name="file-gif" verb="GET,HEAD" path="/public/*.gif" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode"/>
- <add name="file-cc" verb="GET,HEAD" path="/public/*.cc" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode"/>
- </handlers>
- <!--<validation validateIntegratedModeConfiguration="false" />-->
- <!--<modules runAllManagedModulesForAllRequests="true" />-->
- </system.webServer>
复制代码
一定要加目录。。。。日了狗了,真坑
完美解决!!
|