我如何忽略的路由一些文件类型asp.net 4.0

问题描述:

假设我想,路由应该忽略* .js文件* .css和*。PNG等我如何忽略的路由一些文件类型asp.net 4.0

我搜索谷歌和发现解决方案。我在我的页面实现,但仍然js文件不下载。

这里是我的代码

void Application_Start(object sender, EventArgs e) 
    { 
     RouteTable.Routes.Ignore("images/{*pathInfo}"); 
     RouteTable.Routes.Ignore("Scripts/{*pathInfo}"); 
     RouteTable.Routes.Ignore("Styles/{*pathInfo}"); 


     RouteTable.Routes.MapPageRoute("Source", "UrlRewrite/Approach1/Source/{ID}/{Title}", "~/UrlRewrite/Approach1/Source.aspx"); 
     RouteTable.Routes.MapPageRoute("Source2", "UrlRewrite/Approach1/Source/{Question}/{ID}/{Title}/{Page}", "~/UrlRewrite/Approach1/Source.aspx"); 
     RouteTable.Routes.MapPageRoute("Source1", "Feedback/{ID}/{Title}", "~/Feedback.aspx"); 
     //RouteTable.Routes.MapPageRoute("Source1", "Source.aspx{?}{ID}{&}{Title}", "~/UrlRewrite/Approach1/Source.aspx"); 
     RouteTable.Routes.MapPageRoute("product", "Data/product.aspx/{*ID}", "~/UrlRewrite/Approach1/Source.aspx"); // url mapping with * routing 

    } 

这样告诉我,我的代码是确定用于忽略一些文件类型。为什么我的js没有下载。 我的css文件位于Styles文件夹中,我的js文件位于Script文件夹等。脚本&根文件夹中的样式文件夹。请指导我。感谢

试试这个:

Routes.IgnoreRoute("{file}.js"); 
Routes.IgnoreRoute("{file}.css"); 
Routes.IgnoreRoute("{file}.png"); 
+0

我需要与像路线全路径写.IgnoreRoute( “{脚本/ myjs}的.js”); –

+0

不,只需复制并粘贴我的代码。我用它的.txt文件,它适用于我。 – krolik

按菲尔哈克,http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx

RouteTable.Routes.IgnoreRoute("{*alljs}", new {[email protected]".*\.js(/.*)?"}); 
RouteTable.Routes.IgnoreRoute("{*allpng}", new {[email protected]".*\.png(/.*)?"}); 
RouteTable.Routes.IgnoreRoute("{*allcss}", new {[email protected]".*\.css(/.*)?"}); 
+0

我使用routes.IgnoreRoute(“{* alljs}”,新{alljs = @“。* \。js(/.*)?”}); 我仍然得到相同的错误。这是什么意思。 = @ “* \。JS(/.*)?”。我需要提供我的js文件路径吗? –

+0

不,这不应该是必要的。尝试用'RouteTable.Routes'替换'routes' – JConstantine

可以使用RESOLVEURL

<script type="text/javascript" src='<%= ResolveUrl("~/path to script ")%>'></script>