【问题标题】:Can MVC route resolution mechanism be re-configured?能否重新配置 MVC 路由解析机制?
【发布时间】:2011-09-24 20:43:34
【问题描述】:

我已经实现了自定义 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.cshtml”
  • “~/Test.vbhtml”
  • “~/Test/default.cshtml”
  • “~/Test/default.vbhtml”
  • “~/Test/index.cshtml”
  • “~/Test/index.vbhtml”

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

这是我的视图引擎

        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" };

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

【问题讨论】:

    标签: asp.net-mvc razor virtualpathprovider


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2017-07-12
      • 2021-04-23
      相关资源
      最近更新 更多