前两天写了《关于MVC RouteExistingFiles疑问》,本来希望寻求大佬快速解答,奈何无人问津。

只能查看.NET 源代码,可以使用反编译工具(我用IL spy),也可以在线查看微软提供的:https://referencesource.microsoft.com/
MVC的路由会走UrlRoutingModule,代码在 System.Web/Routing/UrlRoutingModule.cs 中

RouteData routeData = RouteCollection.GetRouteData(context);
GetRouteData的源代码:
 public RouteData GetRouteData(HttpContextBase httpContext) {
if (httpContext == null) {
throw new ArgumentNullException("httpContext");
}
if (httpContext.Request == null) {
throw new ArgumentException(SR.GetString(SR.RouteTable_ContextMissingRequest), "httpContext");
} // Optimize performance when the route collection is empty. The main improvement is that we avoid taking
// a read lock when the collection is empty. Without this check, the UrlRoutingModule causes a 25%-50%
// regression in HelloWorld RPS due to lock contention. The UrlRoutingModule is now in the root web.config,
// so we need to ensure the module is performant, especially when you are not using routing.
// This check does introduce a slight bug, in that if a writer clears the collection as part of a write
// transaction, a reader may see the collection when it's empty, which the read lock is supposed to prevent.
// We will investigate a better fix in Dev10 Beta2. The Beta1 bug is Dev10 652986.
if (Count == ) {
return null;
} bool isRouteToExistingFile = false;
bool doneRouteCheck = false; // We only want to do the route check once
if (!RouteExistingFiles) {
isRouteToExistingFile = IsRouteToExistingFile(httpContext);
doneRouteCheck = true;
if (isRouteToExistingFile) {
// If we're not routing existing files and the file exists, we stop processing routes
return null;
}
} // Go through all the configured routes and find the first one that returns a match
using (GetReadLock()) {
foreach (RouteBase route in this) {
RouteData routeData = route.GetRouteData(httpContext);
if (routeData != null) {
// If we're not routing existing files on this route and the file exists, we also stop processing routes
if (!route.RouteExistingFiles) {
if (!doneRouteCheck) {
isRouteToExistingFile = IsRouteToExistingFile(httpContext);
doneRouteCheck = true;
}
if (isRouteToExistingFile) {
return null;
}
}
return routeData;
}
}
} return null;
}

说实话,没看出来什么。

好吧,依稀记得可以调试源代码,也是查了一下,配置VS,下载PDB文件,就能调试了。具体设置,微软地址:https://referencesource.microsoft.com/setup.html

有类似需求的同学可以参考这篇文章,能详细点:https://blog.csdn.net/egman/article/details/78281424

然而,不知道什么原因,我用的VS2017怎样都无法进入源代码。

于是一个问题就变成了两个问题。

于是又搜其他办法,发现JB的DotPeek软件可以,官方地址:https://www.jetbrains.com/decompiler/

这里就不详细介绍操作了,可以参考园友博客:https://www.cnblogs.com/herenwei-wayne/p/5595429.html

run起来,终于进入到源码中GetRouteData方法中了

依次请求静态资源地址,进行调试:

①/a.jpg:匹配到默认路由“controller/action/id”,controller是a.jpg
②/Theme/a.jpg:匹配到默认路由“controller/action/id”,controller是Theme,Action是a.jpg
③/Areas/Admin/a.jpg:匹配到默认路由“controller/action/id”...
④/Areas/Admin/Theme/a.jpg:未匹配到任何路由,renturn

之前的理解是,

1.配到到路由,比如输入a/b/1,但未找到对应的控制器等,提示不存在

2.未匹配到路由,比如输入a/b/c/d/e/f,也会提示不存在

但事实上,

1.配到到路由,比如输入a/b/1,但未找到对应的控制器等,提示不存在(程序可以捕获)

2.未匹配到路由,比如输入a/b/c/d/e/f,路由中return,交由IIS处理

所以,如果利用此特性做文件夹的访问控制,一定要设置对应文件夹的路由规则,然后处理。

关于MVC RouteExistingFiles疑问后续的更多相关文章

  1. 关于MVC RouteExistingFiles疑问

    如图,使用ajpg模拟静态资源.使用 routes.RouteExistingFiles = true; 使静态资源也Map Route,匹配不到则显示Not Foud. ①/a.jpg:访问不到 ② ...

  2. ASP.NET MVC RouteExistingFiles

    遇到这样一个问题:项目是 MVC,但也包含 WebForm 的页面,RouteConfig 中设置了这样一个路由: routes.MapRoute( name: "SubjectIndex& ...

  3. php源码建博客4--实现MVC结构微型框架

    主要: 常量优化路径 自动加载类 优化入口文件 安全访问项目目录 --------------文件结构:-------------------------------------- blog├─App ...

  4. 用户界面编程模式 MVC MVP MVVM

    用户界面编程模式 MVC MVP MVVM 程序 = 数据 + 算法 数据:就是待处理的东西 算法:就是代码 涉及到人机交互的程序,不可避免涉及到界面和界面上显示的数据原始方式是界面代码和逻辑代码糅合 ...

  5. AppClassLoader和WebAppClasssLoader的坑

    最近,打算学习一下spring mvc,为后续做一些积累. 搭建spring+mybatis,动态创建mapper,mapper的文件名称和类在一个目录,但是我之前犯个 错误,大小写写错了,结果我用普 ...

  6. 【EF6学习笔记】(三)排序、过滤查询及分页

    本篇原文地址:Sorting, Filtering, and Paging 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整:并且根据自己的理解做了 ...

  7. EF6 学习笔记(三):排序、过滤查询及分页

    EF6 学习笔记索引目录页: ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 上篇:EF6 学习笔记(二):操练 CRUD 增删改查 本篇原文地址:Sorting, Filterin ...

  8. Activiti进行时——企业工作流生命周期贯通 (zhuan)

    http://www.jianshu.com/p/e6971e8a8dad ********************************************** 图1:一个典型的审批工作流程 ...

  9. Java 注解之总结

    注解是Spring和Mybatis框架所大量使用的技术,要想掌握框架相关技术,注解是必须要掌握的. 掌握注解的优势: 1.能够读懂别人写的代码,特别是框架相关的代码. 2.本来可能需要很多配置文件,需 ...

随机推荐

  1. vue的插槽slot

    插槽是写在子组件上,用啦留给父级添加内容的位置接口: 1. 父级里的 <template :is='子标签名'>父插入内容</template>标签,里的内容       sl ...

  2. yum安装openjdk

    含有的命令:yum,java -version,javac,source,vim [root@ycj ~]# yum -y install java-1.8.0-openjdk-devel //安装j ...

  3. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  4. jsp (二) 练习

    package cn.sasa.serv; import java.io.IOException; import java.sql.SQLException; import java.util.Lis ...

  5. SpringBoot内置Tomcat缓存文件目录被意外删除导致异常

    在项目中,一般会将文件临时保存到缓存目录 当时使用 File.createTempFile("tmp", ext, (File) request.getServletContext ...

  6. Java 基础 IO流

    一,前言 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 二,字节流 2.1 字节输出流 OutputStrea ...

  7. Linux下安装pymysql

    直接使用pip进行安装: [root@mycentos ~]#pip install pymysql

  8. 编译器将"+"转换成了StringBuilder类

    MapReduce map100% Reduce 66% 卡死 如果你碰到map100%,reduce 66% 然后程序就貌似停止在这里了,可能是由于在Reduce类里使用了String造成的 根据一 ...

  9. redis示例 - 限速器,计时器

    INCR INCR key 将 key 中储存的数字值增一. 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. 如果值包含错误的类型,或字符串类型的值不能表示 ...

  10. VC++运行库 集32位/64位整合版

    运行程序时,win7/win10(x86和x64)常会遇到缺少什么缺少msvc***.dll问题 安装下面链接提供的程序,安装后,便可解决. [2016-10-10]Microsoft Visual ...