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

时间:2011-09-24 20:43:35

标签: asp.net-mvc razor virtualpathprovider

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

  • “〜/ Example / Action / 5”
  • “〜/实施例/动作/ 5.cshtml”
  • “〜/实施例/动作/ 5.vbhtml”
  • “〜/实施例/ Action.cshtml”
  • “〜/实施例/ Action.vbhtml”
  • “〜/ Example.cshtml”
  • “〜/ Example.vbhtml”
  • “〜/实施例/动作/ 5 / default.cshtml”
  • “〜/实施例/动作/ 5 / default.vbhtml”
  • “〜/实施例/动作/ 5 / index.cshtml”
  • “〜/实施例/动作/ 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.cshtml”
  • “〜/ Test.vbhtml”
  • “〜/测试/ default.cshtml”
  • “〜/测试/ default.vbhtml”
  • “〜/测试/ index.cshtml”
  • “〜/测试/ 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" };

所以问题是这些默认路由可以删除吗?

1 个答案:

答案 0 :(得分:0)

他们可能与RouteCollection.RouteExistingFiles财产有关吗?我检查大量文件而不仅仅是匹配的文件是没有意义的,但是可能值得关闭以查看它是否有任何区别。