Why does an MVC view need to exist in the View directory to work?

血红的双手。 提交于 2019-12-06 05:52:29

问题


I have been writing a cms with MVC being used as the main engine for generating the pages.

I am going well, but wanted the ability to create a unique razor template per site and possibly per view if I need to.

My rules are that each project has to have a unique code which is linked with a url.

Assets for each project site are stored in a way that the location relates to the project.

So an assets associated with project C0001 would be stored in assets\C0001\ and for C0002: assets\C0002\ and so on.

What I wanted to do, to keep things tidy, was to have the razor templates associated with a project located in the assets\[ProjectCode] location too, but the problem is I am getting an error about ViewBag not existing in context.

So this won't work:

Layout = string.Concat("~/assets/",ViewBag.ProjectNumber,"/_Layout.cshtml");

Where as the following will render a page:

Layout = string.Concat("~/Views/Shared/_",ViewBag.ProjectNumber,"Layout.cshtml");

I am guessing the first layout doesnt render, because it is outside of the known search areas for views? But as I am telling it where the file is, I dont see what the problem is?

I am happy to work using the code in example 2, but could mean after a fair number of project sites the Shared views diretory will become very busy.

Just wondering if there is a reason why Views need to exist in the Views Directory?


回答1:


You need to copy the web.config that is located in your Views directory and put the copy in your Assets directory. Since you need to supply a full path for layouts this is not a search path issue, it needs the info in the web.config to initialise the view properly.




回答2:


By default, the RazorViewEngine is configured to look in the Views directory.

You can change this by creating your own RazorViewEngine instance with different paths and adding it to ViewEngines.Engines.

Its default paths are

AreaViewLocationFormats = new[] {
    "~/Areas/{2}/Views/{1}/{0}.cshtml",
    "~/Areas/{2}/Views/{1}/{0}.vbhtml",
    "~/Areas/{2}/Views/Shared/{0}.cshtml",
    "~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
AreaMasterLocationFormats = new[] {
    "~/Areas/{2}/Views/{1}/{0}.cshtml",
    "~/Areas/{2}/Views/{1}/{0}.vbhtml",
    "~/Areas/{2}/Views/Shared/{0}.cshtml",
    "~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
AreaPartialViewLocationFormats = new[] {
    "~/Areas/{2}/Views/{1}/{0}.cshtml",
    "~/Areas/{2}/Views/{1}/{0}.vbhtml",
    "~/Areas/{2}/Views/Shared/{0}.cshtml",
    "~/Areas/{2}/Views/Shared/{0}.vbhtml"
};

ViewLocationFormats = new[] {
    "~/Views/{1}/{0}.cshtml",
    "~/Views/{1}/{0}.vbhtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Shared/{0}.vbhtml"
};
MasterLocationFormats = new[] {
    "~/Views/{1}/{0}.cshtml",
    "~/Views/{1}/{0}.vbhtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Shared/{0}.vbhtml"
};
PartialViewLocationFormats = new[] {
    "~/Views/{1}/{0}.cshtml",
    "~/Views/{1}/{0}.vbhtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Shared/{0}.vbhtml"
};


来源:https://stackoverflow.com/questions/4889062/why-does-an-mvc-view-need-to-exist-in-the-view-directory-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!