为什么一个MVC视图需要在浏览目录中存在的工作吗? [英] Why does an MVC view need to exist in the View directory to work?
问题描述
我已被写入与MVC一个CMS被用作用于生成页面的主发动机。
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.
我的规则是每个项目都必须有一个独特的code这是一个网址链接。
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.
因此,与项目C0001相关的资产将被存储在资产\\ C0001 \\
和C0002:资产\\ C0002 \\
等。
So an assets associated with project C0001 would be stored in assets\C0001\
and for C0002: assets\C0002\
and so on.
我想做的事,让事情变得整洁,是与位于资产\\ [项目code]
位置的项目相关的剃刀模板过,但问题是,我得到一个错误约 ViewBag在上下文中不存在
。
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
.
所以这是行不通的:
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?
我很高兴用code例2中工作,但可能意味着经过项目现场的相当数量的共同意见diretory会变得非常繁忙。
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?
推荐答案
您需要复制位于你的查看
目录下的Web.config并把副本你的资产
目录。因为你需要提供一个完整路径布局,这不是搜索路径的问题,它需要在web.config中的信息,以正确初始化视图。
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.
这篇关于为什么一个MVC视图需要在浏览目录中存在的工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!