基于 Razor 的视图看不到引用的程序集 [英] Razor-based view doesn't see referenced assemblies
问题描述
我正在尝试基于另一个程序集中的类创建强类型视图.无论出于何种原因,我的 Razor 视图似乎对我的项目中引用的其他程序集没有任何可见性.例如
I'm attempting to create a strongly-typed view based on a class from another assembly. For whatever reason though, my Razor view doesn't seem to have any visibility of other assemblies referenced on my project. e.g.
@model MyClasses.MyModel
导致 Visual Studio 2010 中的错误,找不到类型或命名空间名称 MyClasses
(您是否缺少 using 指令或程序集引用?)."
results in the error in Visual Studio 2010, "The type or namespace name MyClasses
could not be found (are you missing a using directive or an assembly reference?)."
标准视图引擎中引用的相同类工作正常.我在尝试在视图主体中引用类时遇到了同样的问题.
The same class referenced in the standard view engine works fine. I have the same trouble trying to reference the class in the body of my view.
我是否遗漏了有关 Razor 的某些内容,还是需要以其他方式引用程序集?
Am I missing something about Razor or do I need to reference the assembly some other way?
推荐答案
有一个新的配置部分,用于引用 Razor 视图的命名空间.
There is a new configuration section that is used to reference namespaces for Razor views.
打开 Views
文件夹中的 web.config
文件,并确保它包含以下内容:
Open the web.config
file in your Views
folder, and make sure it has the following:
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="SquishIt.Framework" />
<add namespace="Your.Namespace.Etc" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>
或者,您可以在共享布局中添加 using 语句:
Alternatively, you can add using statements to your shared layout:
@using Your.Namespace.Etc;
<!DOCTYPE html>
<head>
....
编辑 Web.config 后,重新启动 Visual Studio 以应用更改.
After editing the Web.config, restart Visual Studio to apply the changes.
这篇关于基于 Razor 的视图看不到引用的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!