asp.net-mvc 基于 Razor 的视图看不到引用的程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4953330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor-based view doesn't see referenced assemblies
提问by techphoria414
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.
我正在尝试基于另一个程序集中的类创建强类型视图。不管出于何种原因,我的 Razor 视图似乎对我的项目中引用的其他程序集没有任何可见性。例如
@model MyClasses.MyModel
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?)."
导致 Visual Studio 2010 中出现错误“MyClasses
找不到类型或命名空间名称(您是否缺少 using 指令或程序集引用?)”。
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?
我是否遗漏了有关 Razor 的某些信息,或者我是否需要以其他方式引用程序集?
回答by quentin-starin
There is a new configuration section that is used to reference namespaces for Razor views.
有一个新的配置部分用于引用 Razor 视图的命名空间。
Open the web.config
file in your Views
folder, and make sure it has the following:
打开web.config
文件Views
夹中的文件,并确保它包含以下内容:
<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 语句添加到您的共享布局:
@using Your.Namespace.Etc;
<!DOCTYPE html>
<head>
....
After editing the Web.config, restart Visual Studio to apply the changes.
编辑 Web.config 后,重新启动 Visual Studio 以应用更改。
回答by V.B.
I had the same problem: MVC3 Project MyCore.Web was referencing to MyCore.DBLayer namespace from another project in the same solution (with assembly name MyCoreDBLayer). All objects from MyCore.DBLayer worked perfectly in Controllers and Models but failed in Razor views with an error 'The type or namespace name 'DBLayer' does not exist in the namespace 'MyCore' (are you missing an assembly reference?)'which was obviously not the case.
我遇到了同样的问题:MVC3 项目 MyCore.Web 正在从同一解决方案中的另一个项目(程序集名称为 MyCoreDBLayer)引用 MyCore.DBLayer 命名空间。MyCore.DBLayer 中的所有对象在控制器和模型中都能完美运行,但在 Razor 视图中失败,并出现错误“类型或命名空间名称“DBLayer”在命名空间“MyCore”中不存在(您是否缺少程序集引用?)”这是显然不是这样。
- Copy Local option was set to true.
- Adding "using..." statements in Razor views was useless
- Adding namespaces to system.web.webPages.razor section was useless as well
- 复制本地选项设置为 true。
- 在 Razor 视图中添加“使用...”语句是无用的
- 将命名空间添加到 system.web.webPages.razor 部分也是无用的
Adding assembly referecene to system.web/compilation/assemblies section of the root web.config file fixed the issue. The section now looks like:
将程序集引用添加到根 web.config 文件的 system.web/compilation/assemblies 部分修复了该问题。该部分现在看起来像:
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
**<add assembly="MyCoreDBLayer" />**
</assemblies>
</compilation>
...
</system.web>
Omitting version, culture, token was OK for now, but should be fixed in the future.
省略 version、culture、token 暂时没问题,但应该在未来修复。
回答by Sam
In my case, the separate project that contained the namespace was a Console Application. Changing it to a Class Library fixed the problem.
就我而言,包含命名空间的单独项目是一个控制台应用程序。将其更改为类库解决了该问题。
回答by Sylvia
None of the above worked for me either;
以上都不适合我;
- Dlls were set to Copy Local
- Adding namespaces to both web.configs did nothing
- Adding assembly references to system.web\compilation\assemblies also did not help (although I have not removed these references, so it may be that they are needed too)
- Dlls 被设置为复制本地
- 将命名空间添加到两个 web.configs 什么也没做
- 向 system.web\compilation\assemblies 添加程序集引用也没有帮助(虽然我没有删除这些引用,所以可能也需要它们)
But I finally found something that worked for me:
但我终于找到了对我有用的东西:
It was because I had my Build Output going to bin\Debug\ for Debug configuration and bin\Release\ for Release configuations. As soon as I changed the Build Configuation to "bin\" for All configuations (as per image below) then everything started working as it should!!!
这是因为我的构建输出将转到 bin\Debug\ 进行调试配置,并将 bin\Release\ 用于发布配置。一旦我将所有配置的构建配置更改为“bin\”(如下图所示),一切都开始正常工作!!!
I have no idea why separating out your builds into Release and Debug folders should cause Razor syntax to break, but it seems to be because something couldn't find the assemblies. For me the projects that were having the razor syntax issues are actually my 'razor library' projects. They are set as Application Projects however I use them as class libraries with RazorGenerator to compile my views. When I actually tried to run one of these projects directly it caused the following Configuration error:
我不知道为什么将您的构建分离到 Release 和 Debug 文件夹会导致 Razor 语法中断,但这似乎是因为找不到程序集。对我来说,有 razor 语法问题的项目实际上是我的“razor 库”项目。它们被设置为应用程序项目,但是我将它们用作带有 RazorGenerator 的类库来编译我的视图。当我实际尝试直接运行这些项目之一时,它导致了以下配置错误:
Could not load file or assembly 'System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
无法加载文件或程序集“System.Web.Helpers,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。该系统找不到指定的文件。
This lead me to trying to change the Build Output, as I notice that for all web projects the Build Output seems to always be directly to the bin folder, unlike the default for class libraries, which have both release and debug folders.
这导致我尝试更改构建输出,因为我注意到对于所有 Web 项目,构建输出似乎总是直接到 bin 文件夹,这与类库的默认设置不同,后者具有发布和调试文件夹。
回答by Chris Moschini
You seem to be looking for this answer: https://stackoverflow.com/a/4136773/176877
你似乎在寻找这个答案:https: //stackoverflow.com/a/4136773/176877
That is, open the inner Views\Web.Config (NOT the root one), and add the namespace under the Pages tag:
即打开内部的 Views\Web.Config(不是根目录),在 Pages 标签下添加命名空间:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory.../>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
...
<add namespace="System.Web.Routing" />
<!-- Your namespace here -->
</namespaces>
Save that, then close and reopen the Razor file.
保存,然后关闭并重新打开 Razor 文件。
If you're using Areas you'll need to do this for each Web.Config in each Area.
如果您使用区域,则需要为每个区域中的每个 Web.Config 执行此操作。
Visual Studio has gotten buggier over the years so it may require closing the Razor file running a Debug build then reopening the Razor file, or may in the worst require restarting Visual Studio. But ultimately it will present the Razor file to you as though everything in that namespaces list was in @using statements at the top of all your Views.
多年来,Visual Studio 的问题越来越多,因此它可能需要关闭运行调试版本的 Razor 文件,然后重新打开 Razor 文件,或者在最坏的情况下可能需要重新启动 Visual Studio。但最终它将向您呈现 Razor 文件,就好像该名称空间列表中的所有内容都在所有视图顶部的 @using 语句中。
回答by radbyx
In ASP.NET Core MVCthe solution is to add a using
in _ViewImports.cshtml, instead of putting it web.config in the View folder when working with ASP.NET MVC 5.
在ASP.NET Core MVC 中,解决方案是using
在 _ViewImports.cshtml 中添加一个,而不是在使用 ASP.NET MVC 5 时将其 web.config 放在 View 文件夹中。
_ViewImports.cshtml
_ViewImports.cshtml
@using mySolution
@using mySolution.ViewModels // <-- Add this, and place your ViewModel (e.g. LoginViewModel) in here.
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
View
看法
@model LoginViewModel // Add to _ViewImports to make this line work
<div>This is the View for the login screen.</div>
回答by user1619480
For me I was referencing a project that was a console application. It was set to build as an exe (console application) instead of class library (DLL). When I changed this I was able to see the models from that separate project no problem.
对我来说,我指的是一个作为控制台应用程序的项目。它被设置为构建为 exe(控制台应用程序)而不是类库 (DLL)。当我改变它时,我能够从那个单独的项目中看到模型,没问题。
回答by Jawand Singh
well, for me it was different. I was missing assembly of my console application project with MVC project. So, adding reference was not enough.
嗯,对我来说是不同的。我错过了我的控制台应用程序项目与 MVC 项目的组装。因此,仅添加引用是不够的。
well this might help someone else. go to root web.config file system.web
-> compilation
-> add your project reference like this.
那么这可能会帮助别人。转到根 web.config 文件system.web
-> compilation
-> 像这样添加您的项目引用。
<assemblies>
<add assembly="Your.Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</assemblies>
<assemblies>
<add assembly="Your.Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</assemblies>
回答by Geeganage
I also had the same issue, but the problem was with the Target framework of the assembly.
我也遇到了同样的问题,但问题出在程序集的Target 框架上。
The referenced assembly was in .NET Framework 4.6 where the project has set to .NET framework 4.5.
引用的程序集位于 .NET Framework 4.6 中,其中项目已设置为 .NET Framework 4.5。
Hope this will help to someone who messed up with frameworks.
希望这会对那些搞砸框架的人有所帮助。
回答by Jens Tand
Your Project FOLDER name needs to be the same. If your Project or Solution name is different, then MVC will hurt you.
您的项目文件夹名称必须相同。如果您的项目或解决方案名称不同,那么 MVC 会伤害您。
Example : If you create a new Application and it gets the default name Webapplicaiton1, then this namespace will be created. So, let us say that you dont want to have this namespace, so from the VS you change everywhere you can see to "MyNamespace". You also search and replace all code from "Webapplication1" and replace it with "MyNamespace".
This also changes web.config file, so that it inculdes
示例:如果您创建一个新应用程序并且它获得默认名称 Webapplicaiton1,则将创建此命名空间。所以,让我们说你不想拥有这个命名空间,所以从你可以看到的 VS 处更改为“MyNamespace”。您还可以搜索并替换“Webapplication1”中的所有代码,并将其替换为“MyNamespace”。这也会更改 web.config 文件,以便它包含
Now everything will work, except Razor views.
现在一切正常,除了 Razor 视图。
RazorViews cannot find it, because there is some kind of strange dependency on the FOLDERNAME of the project. It is terrible design.
RazorViews 找不到它,因为对项目的 FOLDERNAME 有某种奇怪的依赖。这是可怕的设计。
I have tested this semi-thoroughly by copying my files into a new solution, and the only difference being the foldername.
我已经通过将我的文件复制到新解决方案中来半彻底地测试了这一点,唯一的区别是文件夹名称。