MVC路由解析机制可以重新配置吗?

发布于 2024-12-06 20:55:32 字数 2144 浏览 1 评论 0原文

我已经实现了自定义 VirtualPathProvider 来提供来自数据库的可自定义视图,当我在 FileExists 方法上放置断点时,我注意到该框架执行了大量不必要的(对于我的项目)请求。例如,当我请求不存在的操作时(例如 http://localhost/Example/Action)框架查找:

  • “~/Example/Action/5”
  • “~/Example/Action/5.cshtml”
  • “~/Example/Action/5.vbhtml”
  • “~/Example/Action.cshtml”
  • “~/Example/Action.vbhtml”
  • “~/Example.cshtml”
  • “~/Example.vbhtml”
  • “~/Example/Action/5/default.cshtml”
  • “~/Example/Action/5/default.vbhtml”
  • “ ~/Example/Action/5/index.cshtml"
  • "~/Example/Action/5/index.vbhtml"
  • "~/favicon.ico"
  • “~/favicon.ico.cshtml”
  • “~/favicon.ico.vb​​html”
  • “~/favicon.ico/default.cshtml”
  • “~/favicon.ico/default.vbhtml”
  • “~/favicon.ico/index.cshtml "
  • "~/favicon.ico/index.vbhtml"

当我发出与添加的路由匹配的请求时(例如 http://localhost/Test) 框架查找:

  • "~/Test"
  • " ~/Test.cshtml
  • " "~/Test.vbhtml"
  • "~/Test/default.cshtml "
  • "~/Test/default.vbhtml"
  • "~/Test/index.cshtml"
  • "~/Test/index.vbhtml"

在初始化控制器之前。控制器初始化后,框架会查找我实现的自定义 RazorViewEngine 中定义的视图。

这是我的 ViewEngine

        AreaViewLocationFormats = new string[] { };
        AreaMasterLocationFormats = new string[] { };
        AreaPartialViewLocationFormats = new string[] { };
        MasterLocationFormats = new string[] { }; 
        ViewLocationFormats = new string[] { 
            "~/Views/Dynamic/{1}/{0}.cshtml",
            "~/Views/Dynamic/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
        PartialViewLocationFormats = new string[] { 
            "~/Views/Dynamic/{1}/Partial/{0}.cshtml",
            "~/Views/Dynamic/Shared/Partial/{0}.cshtml",
            "~/Views/{1}/Partial/{0}.cshtml",
            "~/Views/Shared/Partial/{0}.cshtml"
        };
        FileExtensions = new string[] { "cshtml" };

所以问题是这些默认路由是否可以删除以及如何删除?

I have implemented custom VirtualPathProvider to serve customizable Views from a DB and when i put a breakpoint on the FileExists method I noticed that the framework does ton of unnecessary (for my project) requests. For example when I make a request for non-existing action (e.g. http://localhost/Example/Action) the framework looks for:

  • "~/Example/Action/5"
  • "~/Example/Action/5.cshtml"
  • "~/Example/Action/5.vbhtml"
  • "~/Example/Action.cshtml"
  • "~/Example/Action.vbhtml"
  • "~/Example.cshtml"
  • "~/Example.vbhtml"
  • "~/Example/Action/5/default.cshtml"
  • "~/Example/Action/5/default.vbhtml"
  • "~/Example/Action/5/index.cshtml"
  • "~/Example/Action/5/index.vbhtml"
  • "~/favicon.ico"
  • "~/favicon.ico.cshtml"
  • "~/favicon.ico.vbhtml"
  • "~/favicon.ico/default.cshtml"
  • "~/favicon.ico/default.vbhtml"
  • "~/favicon.ico/index.cshtml"
  • "~/favicon.ico/index.vbhtml"

When I make a request that matches an added route (e.g http://localhost/Test) the framework looks for:

  • "~/Test"
  • "~/Test.cshtml"
  • "~/Test.vbhtml"
  • "~/Test/default.cshtml"
  • "~/Test/default.vbhtml"
  • "~/Test/index.cshtml"
  • "~/Test/index.vbhtml"

before even initialising the controller. After the controller is initialised the framework looks for the view as defined in the custom RazorViewEngine that I have implemented.

This is my ViewEngine

        AreaViewLocationFormats = new string[] { };
        AreaMasterLocationFormats = new string[] { };
        AreaPartialViewLocationFormats = new string[] { };
        MasterLocationFormats = new string[] { }; 
        ViewLocationFormats = new string[] { 
            "~/Views/Dynamic/{1}/{0}.cshtml",
            "~/Views/Dynamic/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
        PartialViewLocationFormats = new string[] { 
            "~/Views/Dynamic/{1}/Partial/{0}.cshtml",
            "~/Views/Dynamic/Shared/Partial/{0}.cshtml",
            "~/Views/{1}/Partial/{0}.cshtml",
            "~/Views/Shared/Partial/{0}.cshtml"
        };
        FileExtensions = new string[] { "cshtml" };

So the question is can these default routes be removed and how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼忆七猫命九 2024-12-13 20:55:32

它们是否与 RouteCollection 有关。 RouteExistingFiles 属性?我认为它检查大量文件而不仅仅是匹配一个文件是没有意义的,但可能值得关闭它看看它是否有任何区别。

Could they be related to the RouteCollection.RouteExistingFiles property? I doesn't make sense for it to check for lots of files rather than just one that matches, but it might be worth turning off to see if it makes any difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文