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
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?
Try adding the namespace your
MyClasses
is in to the web.config under<pages> <namespaces></namespaces> </pages>
There is a new configuration section that is used to reference namespaces for Razor views.
Open the
web.config
file in yourViews
folder, and make sure it has the following:Alternatively, you can add using statements to your shared layout:
After editing the Web.config, restart Visual Studio to apply the changes.
In my case, the separate project that contained the namespace was a Console Application. Changing it to a Class Library fixed the problem.
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. ofSystem.Web.WebPages.Razor
, etc.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)
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.