基于剃刀视图没有看到引用的程序集 [英] 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.
我失去了一些关于剃刀或做我需要引用大会一些其他的方式?
Am I missing something about Razor or do I need to reference the assembly some other way?
推荐答案
有是用于引用命名空间剃刀意见的新的配置部分。
There is a new configuration section that is used to reference namespaces for Razor 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>
另外,你可以添加使用语句来共享布局:
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.
这篇关于基于剃刀视图没有看到引用的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!