Namespace missing when aspx pages being compiled. MVC2, .Net4 - model-view-controller

I have my data model in project A, in the namespace ProjectA.Model.MyEntities . In project B (an MVC2 project), I reference project A. But when my pages are requested, they are choking saying that 'type or namespace ProjectA is not known.
When I compile my project, I get no errors. From my controller, the database context seems to be working fine, as references to MyEntities compile fine. Going to definition on MyEntities navigates me to the Model.Designer.cs file in project A.
It's like project A is reference during static compile, but not at run time.
This is an MVC2 project, running on .Net4, in Visual Studio 2010, using ASPX engine. It is a legacy project, that is why it is MVC2, but it has been upgraded to .Net4.
What do I have to do to get rid of this error. Have been beating my head on the wall over it all morning.

You could try adding the namespace for ProjectA.Model.MyEntities to the web.config, this will inform the application that the namespace is required for all views.
<system.web>
<pages>
<namespaces>
<!-- defaults omitted for clarity-->
<add namespace="ProjectA.Model.MyEntities" />
</namespaces>
</pages>
</system.web>
Alternatively, you can use an imports directive at the top of the individual views - this would be more appropriate if you are only using ProjectA's entities within a limited number of views:
<%# Import Namespace="ProjectA.Model.MyEntities"%>
This should be included after the #Page or #Control directive.

Related

Suppressing Microsoft Fakes warnings

I'm using Microsoft Fakes to shim a couple WindowsAzure components for testing. Following the advice in vs 2012: Shims compile, I updated my .fakes file to just generate the shims I actually need:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="false">
<Assembly Name="Microsoft.WindowsAzure.Storage" Version="2.1.0.0"/>
<StubGeneration>
<Clear/>
</StubGeneration>
<ShimGeneration>
<Clear/>
<Add FullName="Microsoft.WindowsAzure.Storage.CloudStorageAccount"/>
<Add FullName="Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient"/>
<Add FullName="Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer"/>
<Add FullName="Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob"/>
<Add FullName="Microsoft.WindowsAzure.Storage.Queue.CloudQueueClient"/>
<Add FullName="Microsoft.WindowsAzure.Storage.Queue.CloudQueue"/>
</ShimGeneration>
</Fakes>
But I'm still getting the "Some fakes could not be generated..." warning. All the specified shims are being generated, and commenting any of those above lines out causes my test project to fail to build. If I turn on diagnostics, I see dozens of messages like:
Warning 2 Cannot generate shim for Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient+<>c__DisplayClass1: type is not supported because of internal limitations.
Everything works, I just want to suppress the warning so it stops confusing our CI server. Is there a warning number for the non-diagnostic message I can just stick in the test project to ignore?
You can remove types from the shimgeneration using
<Remove TypeName="c__DisplayClass" />
That will remove out all the types containing the above string.
See msdn link
I solved this by going into my Fakes folder and deleting the fakes for that assembly, then going into the References folder and deleting the fakes DLL for that assembly. Then I right-clicked on the assembly in references, and chose Add Fakes Assembly (again).
After it did all the fakes creation stuff (takes a few minutes), I built the project again and all the errors went away.

Using PrecompiledMvcEngine FindView throws InvalidOperationException and looks for View cshtml files

I got an InvalidOperationException when the MVC controller tried to FindView while using the PrecompiledMvcEngine.
Using on a machine that has VS2012, MVC4 and deployed to IIS
After looking at the source code of ControllerBase.FindView to ViewEngineCollection.FindView to VirtualPathProviderViewEngine to PrecompiledMvcEngine, found that _mappings in PrecompiledMvcEngine had a count of 0. (Tx to Reflection and Open source.)
The reason is that there was not Type assignable to WebPageRenderingBase in my project's DLL. On decompiling my dll, it actually had the compiled views, and the views extended from WebPageRenderingBase.
After writing a unit test to do the same thing that PrecompiledMvcEngine does to load views, found that they are using different versions of the WebPageRenderingBase class. RazorGenerator.Mvc uses System.Web.WebPages v1.0.0.0 to precompile at build time. The PrecompiledMvcEngine uses System.Web.WebPages v2.0.0.0 when loading types from the compiled assembly. Fixed this by changing my MVC csproject to also load v1.0.0.0 at runtime.
Changed
<Reference Include="System.Web.WebPages" />
which picked up 2.0.0.0 to
<Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
forcing it to always pick up 1.0.0.0. This problem will often be faced if you are deploying precompiled views to a machine with VS2012 and MVC4 installed, without specifying the correct version in the csproj file

System.Web.Helpers does not exist in namespace: WebImage

While I know this has been "answered" about 50 million times, I haven't found an answer that fixes the issue for me, so I feel like I have no choice but to ask again.
Previous suggestions:
Razor pages in MVC are giving a compile error with System.Web.Helpers not being found
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
I have added this to my web.config, but the issue remains.
Type or namespace name does not exist
I only have one project, and the targetFramework is 4.0.
I installed Microsoft.Web.Helpers 1.15 (2.0 is incompatible with MVC3.) The Microsoft.Web.Helpers.dll is in my bin folder.
The odd thing is that there's Microsoft.Web.Helpers and System.Web.Helpers. I need System.Web.Helpers in this case, because I'm using WebImage in my code.
As I keep telling people, it's when I ask for help that I find the answer. In my case, somehow in the process of destroying my Solution earlier by upgrading to Microsoft ASP.NET Helper Library 2.0 and then trying to undo that action, System.Web.Helpers stopped being a referenced assembly in my project. Simple fix!
Of course, fixing that led me to the next Assembly Reference error. I think I'll be here all night.

Could not load file or assembly 'System.Data.Entity

I am working within a Solution (a jokes website). The Solution has 2 Projects:
Model (C# Class Library)
MVC 3 Empty Application
I am trying to get my view to list the Jokes in the Database, but I get the following error:
Could not load file or assembly 'System.Data.Entity, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The system cannot find the file specified
As you can see from the Error message, I have already added the System.Data.Entity to the web.config of the MVC 3 application.
No matter what I do, I seem to be unable to fix the error! I have added using statements for the Entity class, to the HomeController and the Index.cshtml.
To use an external Entity Framework model (embed in a DLL for example) with ASP.NET MVC 3 you must :
Add the following reference to your MVC project : System.Data.Entity (Version 4.0.0.0, Runtime v4.0.30319)
Add the following line in your web.config
...
< compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
To resolve this error:
Go to references section in your project
Right click it and than go add library package reference.
search in online tab for entity framework
you will get a lot of installed packages if u have internet connection enabled
Select EF4 package, and finally, add it
If you have any entity frame work installed and you are getting an error then click for add reference and in Browse tab go to below location:
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
Select to find System.Data.Entity.dll and then add it. This will resolve this issue.
I was getting the same error, and it was because the MVC3 project used Entity Framework 4.1, and my domain model (in a class library project, same solution) grabbed 4.2 when I added the reference.
I uninstalled EF from my Web project, then reinstalled, now both have 4.2 and are working fine.
Currently working with the Apress title, Pro ASP.NET MVC Framework (Freeman).
another way to solve this is to add empty edmx file (Add -> Class -> Data -> ADO.NET Entity Data Model) and delete it afterwards.
I am not sure what the Visual Studio Wizard does, but it`s a common problem at my machine and i always fix it like that.
Make sure you have referenced the System.Data.Entity assembly in your project. Not only in the web.config assemblies section but also those assemblies being referenced. Also make sure that the System.Data.Entity, V4.0.0.0 is present in the GAC on the server you are running this application.

Migrating MVC2 to MVC3: problem with MVCFutures

I'm trying to migrate an MVC2 project to MVC3 - i've followed the instructions within the release notes and the app will compile, but the project depends on a few helpers located inside the previous MVCFutures and tosses:
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'SubmitImage' and no extension method 'SubmitImage' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
My solution includes a Lib folder, inside of which is:
MVCFutures\Microsoft.Web.Mvc.dll
Do i have an upgrade option here?
use this tools for upgrading:
http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx
#justSteve,
First off, you can download the MVC3 source from codeplex, which includes the MvcFutures code, and just build the new futures assembly yourself.
However, I'd be willing to bet that your problem is a missing using statement somewhere.
Make sure your page has a using namespace statement for Microsoft.Web.Mvc.
If you're trying to migrate to Razor (which you didn't say, but just in case), you can put a namespace reference in the web.config file contained in your Views folder (to avoid having to include the using statement in each of your views), like this:
<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="Microsoft.Web.Helpers" />
...
</namespaces>
</pages>
</system.web.webPages.razor>
You mentioned that your MVC futures binary is located in a Lib folder. Make sure that:
Your project has an assesmbly reference to that library
The MvcFutures dll is being copied to the bin folder
Your web.config has an assembly reference to MvcFutures
Could you also clarify if the exception you are seeing is when compiling your app in Visual Studio (because you are using MVC futures methods in your controllers or data models) or when you access your application in the browser (because you are using futures in your views).
Btw, an upgraded version of MVC Futures that targets MVC 3 will be released soon.
Update: The source code for MVC 3 Futures is already available on codeplex: http://aspnet.codeplex.com/releases/view/58781 so you can compile it yourself.
For upgrading your views you might want to try this from Telerik...
https://github.com/telerik/razor-converter
...it will convert your webforms views to razor.
Regards
Paul

Resources