Razor 기반보기에 참조 된 어셈블리가 표시되지 않음
다른 어셈블리의 클래스를 기반으로 강력한 형식의 뷰를 만들려고합니다. 그러나 어떤 이유로 든 내 Razor 뷰에는 내 프로젝트에서 참조되는 다른 어셈블리가 표시되지 않는 것 같습니다. 예 :
@model MyClasses.MyModel
Visual Studio 2010에서 "유형 또는 네임 스페이스 이름을 MyClasses
찾을 수 없습니다 (using 지시문 또는 어셈블리 참조가 누락 되었습니까?)"라는 오류가 발생합니다.
표준보기 엔진에서 참조 된 동일한 클래스가 제대로 작동합니다. 내 견해의 본문에서 클래스를 참조하는 데 동일한 문제가 있습니다.
Razor에 대해 뭔가 빠졌거나 다른 방식으로 어셈블리를 참조해야합니까?
Razor 뷰의 네임 스페이스를 참조하는 데 사용되는 새로운 구성 섹션이 있습니다.
폴더 에서 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>
또는 공유 레이아웃에 using 문을 추가 할 수 있습니다.
@using Your.Namespace.Etc;
<!DOCTYPE html>
<head>
....
Web.config를 편집 한 후 Visual Studio를 다시 시작하여 변경 사항을 적용합니다.
동일한 문제가 발생했습니다. MVC3 Project MyCore.Web이 동일한 솔루션 (어셈블리 이름 MyCoreDBLayer 사용)의 다른 프로젝트에서 MyCore.DBLayer 네임 스페이스를 참조했습니다. MyCore.DBLayer의 모든 개체는 컨트롤러 및 모델에서 완벽하게 작동했지만 Razor 뷰에서는 'The type or namespace name'DBLayer 'does not exist in the namespace'MyCore '(A you missing an assembly reference?)' 라는 오류와 함께 실패했습니다. 분명히 그렇지 않습니다.
- 로컬 복사 옵션이 true로 설정되었습니다.
- Razor 뷰에서 "using ..."문을 추가하는 것은 쓸모가 없었습니다.
- system.web.webPages.razor 섹션에 네임 스페이스를 추가하는 것도 쓸모가 없었습니다.
루트 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>
버전, 문화, 토큰 생략은 당분간 괜찮 았지만 향후 수정되어야합니다.
제 경우에는 네임 스페이스가 포함 된 별도의 프로젝트가 콘솔 애플리케이션이었습니다. 클래스 라이브러리로 변경하면 문제가 해결되었습니다.
위의 어느 것도 나를 위해 일하지 않았습니다.
- DLL이 로컬 복사로 설정되었습니다.
- 두 web.configs에 네임 스페이스를 추가하면 아무 작업도 수행되지 않았습니다.
- system.web \ compilation \ assemblies에 어셈블리 참조를 추가하는 것도 도움이되지 않았습니다 (이 참조를 제거하지는 않았지만 필요하기 때문일 수 있음).
그러나 마침내 나는 나를 위해 일한 것을 발견했습니다.
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!!!
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:
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.
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.
You seem to be looking for this answer: 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:
<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.
If you're using Areas you'll need to do this for each Web.Config in each Area.
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.
In ASP.NET Core MVC the 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.
_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>
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.
I was getting the same error while trying to use Smo objects in a Razor view. Apparently this is because Razor can't find the DLLs referenced in the project. I resolved this by setting "Copy Local" to true for all Smo dlls, however there might be a better solution (see Czechdude's link above) @using and web.config edits are useless because they are only needed if you wish to omit the namespace part from the type names (for instance Server instead of Microsoft.SqlServer.Management.Smo.Server)
I was getting a similar error after moving my dev machine from Win7 32bit to Win7 64bit. Error message:
...\Web\Views\Login.cshtml: ASP.net runtime error: [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from System.Web.WebPages.Razor, Version=1.0.0.0 ... Type B originates from ... Version=2.0.0.0
Turns out I had both versions in the GAC. The View web.config
referenced v1 but the app was referencing v2. Removed the referenced assemblies and re-added v1. of System.Web.WebPages.Razor
, etc.
well, for me it was different. I was missing assembly of my console application project with MVC project. So, adding reference was not enough.
well this might help someone else. go to root web.config file system.web
-> compilation
-> add your project reference like this.
<assemblies> <add assembly="Your.Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </assemblies>
I also had the same issue, but the problem was with the Target framework of the assembly.
The referenced assembly was in .NET Framework 4.6 where the project has set to .NET framework 4.5.
Hope this will help to someone who messed up with frameworks.
Your Project FOLDER name needs to be the same. If your Project or Solution name is different, then MVC will hurt you.
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
Now everything will work, except Razor views.
RazorViews cannot find it, because there is some kind of strange dependency on the FOLDERNAME of the project. It is terrible design.
I have tested this semi-thoroughly by copying my files into a new solution, and the only difference being the foldername.
Try adding the namespace your MyClasses
is in to the web.config under
<pages> <namespaces></namespaces> </pages>
include the entire namespace
@model namespace.myclasses.mymodel
None of these https://stackoverflow.com/a/7597360/808128 do work for me. Even "adding assembly referecene to system.web/compilation/assemblies section of the root web.config file". So the two ways remains for me: 1) to add a public wrap class for my assembly that Razor code can access to this assembly through this wrap; 2) just add assembly logic to a public class in the same assembly where the Razor's code is located.
In addition to making the web.config changes for <assemblies>
and <namespaces>
, I found that GAC'ing the assembly made a big difference. You can apply culture and public key token like any core .NET assembly that is registered globally.
Some may shudder at the mention of the GAC. But as a BizTalk developer I've grown to embrace it.
This solution worked for me (It's funny, but works)
I edited the view pages and copied the contents and pasted in it,i didn't change any content of the views, but just edited so the visual studio could do it's thing to track the pages, and afterwards every thing started working
Solution - Just edit the pages and replace with the same pages (Worked for me)
in spacename models, yourClassModel, add public before name class
public class yourClassModel{
prop
}
참고URL : https://stackoverflow.com/questions/4953330/razor-based-view-doesnt-see-referenced-assemblies
'development' 카테고리의 다른 글
Elasticsearch : "용어", "일치 구문"및 "쿼리 문자열"의 차이점 (0) | 2020.08.21 |
---|---|
Vim은 파일의 실시간 변경 사항을 모니터링 할 수 있습니다. (0) | 2020.08.21 |
R을 사용하여 그래프를 .eps 파일로 내보내기 (0) | 2020.08.21 |
java.util.List를 Scala 목록으로 변환하는 방법 (0) | 2020.08.21 |
Ehcache에서 TTL과 유휴 시간을 구별하는 방법 (0) | 2020.08.21 |