首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

asp.net mvc RouteCollection的RouteExistingFiles属性了解

2012-11-23 
asp.net mvc RouteCollection的RouteExistingFiles属性理解RouteCollectiond的RouteExistingFiles属性一看

asp.net mvc RouteCollection的RouteExistingFiles属性理解

RouteCollectiond的RouteExistingFiles属性一看这个名字,我想大家就能猜出来它的意思,对静态资源是否启用路由。我在Asp.net Web.config文件读取路径你真的清楚吗?里面做demo时遇到这样一个问题:

项目结构如下:

asp.net mvc RouteCollection的RouteExistingFiles属性了解

我原本是用来让程序读views/channel/men/web.config文件,当我添加了men文件夹后,整过路由都出错了。

我的路由代码:

  routes.MapRoute("channelDefault", "{ChannelName}/{action}", new
            {
                controller = "Channel",
                action = "Default"
            });

controller代码:

public ActionResult Default()
        {
            string channelName = RouteData.Values["ChannelName"].ToString().Trim();
            string actionName = RouteData.Values["action"].ToString().Trim();
            string viewName = string.Format("{0}/{1}", channelName, actionName);
            return View(viewName);
        }

整个代码是不是都很简单啊。当我添加了men文件夹后路由出错,移除men路由就对了。那么问题也就找到了       routes.RouteExistingFiles = true;我对RouteExistingFiles树立的理解是:对已存在文件是否启用路由,其实这个理解有点不对啊。

让我们来看看RouteCollection的GetRouteData方法,里面有这么一段:

 private bool CacheLookupOrInsert(string virtualPath, bool isFile)    {        string physicalPath = HostingEnvironment.MapPathInternal(virtualPath);        bool doNotCacheUrlMetadata = CachedPathData.DoNotCacheUrlMetadata;        string key = null;        if (!doNotCacheUrlMetadata)        {            key = this.CreateCacheKey(isFile, physicalPath);            bool? nullable = HttpRuntime.CacheInternal[key] as bool?;            if (nullable.HasValue)            {                return nullable.Value;            }        }        bool flag2 = isFile ? File.Exists(physicalPath) : Directory.Exists(physicalPath);        if (!doNotCacheUrlMetadata)        {            CacheDependency dependencies = null;            string filename = flag2 ? physicalPath : FileUtil.GetFirstExistingDirectory(AppRoot, physicalPath);            if (filename != null)            {                dependencies = new CacheDependency(filename);                TimeSpan urlMetadataSlidingExpiration = CachedPathData.UrlMetadataSlidingExpiration;                HttpRuntime.CacheInternal.UtcInsert(key, flag2, dependencies, Cache.NoAbsoluteExpiration, urlMetadataSlidingExpiration);            }        }        return flag2;    }

这里其中有string physicalPath = HostingEnvironment.MapPathInternal(virtualPath);这句,就是把虚拟路径转化为物理路径的。例如我实验的时候virtualPath=~/men/,而physicalPath=C:\Users\majiang\Documents\Visual Studio 2010\Projects\System.Configuration\MvcApp\men\,这个文件没有但是目录却是有的。顺便提一下默认情况下CachedPathData.DoNotCacheUrlMetadata是false,在这里它主要用来控制是否使用缓存。真正查找文件和目录是否存在是在这句代码完成的:

 bool flag2 = isFile ? File.Exists(physicalPath) : Directory.Exists(physicalPath);不过大家一定要注意这里默认是有缓存的,我当时做实验的时候见了 routes.RouteExistingFiles = true;这句也没起到作用,就知道缓存的存在,知道这里在明白缓存是怎么搞的啊。

总结一下吧:RouteCollectiond的RouteExistingFiles属性是:对已存在的文件和文件夹是否启用路由,大家千万不要忘记还有文件夹这个检查,同时文件和文件夹的检查结果默认是启用了缓存的。

热点排行
Bad Request.