为什么当布局页面在 ~/Views/Shared 之外时,Razor 看不到 ViewBag、Url.Content 等?
我正在开发一个 CSS 框架,我可以将其作为 NuGet 包放入其他项目中。
为了保持整洁,整个框架 - 视图、样式、图像和母版页/布局页面 - 实际上存储在 ~/CssThing/ 下
与 WebForms 视图引擎完美配合,但是当我将 _layout.cshtml 文件移动到 ~/ CssThing/,然后修改我的 Razor 页面说:
@{
Layout = "~/CssThing/_layout.cshtml";
}
它开始抱怨 ViewBag 未定义,或者 名称“Url”在当前上下文中不存在
,或各种其他一些奇怪的现象表明视图不再从正确的基类继承。
我怎样才能让它发挥作用?
注意:一切都像这样分开的原因是没有办法强制 NuGet 覆盖现有代码,并且没有办法在不获取所有 jQuery 等引用的情况下启动空的 MVC3 Web 应用程序,所以不要拿我的框架冒险由于一半的文件已经存在,所以部署一半,我将所有内容完全分开。
I'm working on a CSS framework that I can drop in to other projects as a NuGet package.
To keep things clean, the entire framework - views, styles, images, and master pages / layout pages - is actually stored under ~/CssThing/
Works perfectly with the WebForms view engine, but when I move the _layout.cshtml file into ~/CssThing/ and then modify my Razor page to say:
@{
Layout = "~/CssThing/_layout.cshtml";
}
it starts complaining that ViewBag is not defined, or that The name 'Url' does not exist in the current context
, or various other bits of weirdness that suggest the view is no longer inheriting from the proper base class.
How can I get this to work?
NOTE: The reason everything's split out like this is there's no way to force NuGet to overwrite existing code, and there's no way to spin up an empty MVC3 web application without picking up all the jQuery, etc. references, so rather than risk my framework getting half-deployed because half the files were already present, I'm keeping everything completely seperate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查新视图目录中是否有 web.config。最好复制视图中默认项目创建的项目。
此 web.config 定义了 Razor 的设置,例如默认页面基类以及隐式可用的命名空间。这些命名空间定义了您可以使用哪些 HTML 帮助器。
Check you have a web.config in the new views directory. It's best to copy the one created by the default project in Views.
This web.config defines Razor's settings like default page base class and what namespaces are implicitly available. Those namespaces define what HTML helpers you can use.
您需要手动指定
@inherits WebViewPage
,或添加默认 Web.config 部分(设置基本类型)到包含视图的文件夹。You need to manually specify
@inherits WebViewPage
, or add the default Web.config section (which sets the base type) to the folder with the views.