MVC路由解析机制可以重新配置吗?
我已经实现了自定义 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.vbhtml”
- “~/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们是否与
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.