分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

关于MVC RouteExistingFiles疑问后续

发布时间:2023-09-06 02:31责任编辑:林大明关键词:MVC

前两天写了《关于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 == 0) { ???????????????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疑问后续

原文地址:https://www.cnblogs.com/talentzemin/p/10326415.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved