开发者

Can MVC route resolution mechanism be re-configured?

开发者 https://www.devze.com 2023-04-07 00:45 出处:网络
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 pr

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.c开发者_StackOverflow社区shtml"
  • "~/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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消