为什么MVC视图需要存在于View目录中才能工作?

发布于 2024-10-16 01:10:36 字数 796 浏览 2 评论 0原文

我一直在编写一个cms,使用MVC作为生成页面的主要引擎。

我进展顺利,但希望能够为每个站点甚至每个视图创建一个独特的剃刀模板(如果需要的话)。

我的规则是每个项目都必须有一个与 URL 链接的唯一代码。

每个项目站点的资产都按照位置与项目相关的方式存储。

因此,与项目 C0001 关联的资产将存储在 assets\C0001\ 中,而与项目 C0002 关联的资产将存储在 assets\C0002\ 中,依此类推。

为了保持整洁,我想做的是将剃刀模板与位于 assets\[ProjectCode] 位置的项目关联起来,但问题是我收到了有关 <代码>上下文中不存在ViewBag。

所以这行不通:

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

如下将渲染页面:

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

我猜第一个布局不会渲染,因为它位于已知的视图搜索区域之外?但当我告诉它文件在哪里时,我不明白问题是什么?

我很高兴使用示例 2 中的代码,但这可能意味着在相当数量的项目站点之后,共享视图目录将变得非常繁忙。

只是想知道视图是否需要存在于 Views 目录中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2024-10-23 01:10:36

您需要复制 Views 目录中的 web.config,并将副本放入 Assets 目录中。由于您需要提供布局的完整路径,这不是搜索路径问题,它需要 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.

野侃 2024-10-23 01:10:36

默认情况下,RazorViewEngine 配置为在 Views 目录中查找。

您可以通过使用不同路径创建自己的 RazorViewEngine 实例并将其添加到 ViewEngines.Engines 来更改此设置。

它的默认路径是

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

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"
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文