How to ensure valid version of System.Web.Mvc is used - asp.net-mvc

My hosting provider has MVC 2 Preview 2 installed on server.
How to ensure my app uses MVC 2 Beta dll ?

Add the .dll to a folder in your project, and reference it by browsing to it in the Add Reference dialog. Make sure it gets uploaded with your project.
Using file references will always ensure you're referencing the assembly you want. If you use GAC references, then the reference can change to some other assembly version unless you've specified to use a specific version.

Related

Controllers folder missing Umbraco 8

I installed Umbraco 8.4 as per the instructions found at https://our.umbraco.com/documentation/Getting-Started/Setup/Install/install-umbraco-with-nuget
Once done within Visual Studio 2017 i tried to created a new Controller and the folder (Controllers) was missing.
I'm not sure if i've missed something but i didnt want to create the folder manually in case it introduces new problems later down the line.
I thought to recreate the project but instead of selecting Empty as the project to use MVC but after reading through a few threads it seems the correct way is to select an empty project.
Under the bin directory i do see System.Web.Mvc.dll
Am i missing something?
You should definitely not use the MVC project template when creating a new project - that will add all sorts of dependencies that will likely conflict with what comes with the Umbraco NuGet package.
Go with the "Empty" template and then add in the NuGet and it will give you the dependencies you need for Umbraco to run. If you need to add in other stuff from MVC afterwards, you can add these in a version that aligns with what the Umbraco package has added.
In regards to the Controllers folder - it really doesn't matter where this folder lives or what it is called. Controllers are registered by other means. You are totally fine to create this folder manually and call it whatever you prefer.
I think most people actually prefer to keep Controllers in a completely separate project in the VS solution. Then reference that project by your main project to ensure the compiled DLL is included in your web project - and thereby your controllers can be used by the Umbraco website itself. By doing this, you get a clean separation of your .cs source files so you won't accidentally be deploying those when you deploy your site. To do this you would of course require to add UmbracoCms.Core NuGet package to your other project in order to use Umbraco functionality.
Keep in mind if you are adding plain MVC controllers (not inheriting from the Umbraco base controllers - and therefore not getting automatically registered) you will need to manually register these controllers in the route table in order to access them.
This is no different from what you would do in a normal ASP.NET MVC project, but since this isn't added by default in an Umbraco project - you need to do it yourself.
See this answer for instructions on how to do it: https://stackoverflow.com/a/56972929/336105

Missing reference to System.Web.Mvc in Asp.net Unit Test?

I recently made some unit tests for my Asp.NET MVC application, or I tried to, it keep telling me "The type 'System.Web.Mvc.Controller' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral.' Then I tried to add a reference to the above mentioned, but was only able to choose version 4.0.0.0 and now it tells me that my project already uses version 5.1.0.0, which is a higher version. What to do?
Thanks a bunch!
MVC 5 comes in from a NuGet package. Just as it did with the main MVC web project in your solution. Install MVC via NuGet into your Test project, and you should be good to go.
An educated guess, but you might have obtained the newer version via a nuget package(some of the newer MVC project templates do this), and thus it is not installed in GAC. Use the nuget package manager for the MVC project to determine if this is the case, and if so then mystery solved.
In the test project you can add the same package, or use the addreference->Browse to browse into the nuget dependencies and poke around under the MVC project till you find the DLL(it will be in the file system somewhere under SolutionName/MvcProjectName/packages).

Deploying a Solution with multiple projects on IIS7 - Missing DLLs

I'm trying to publish a solution that contains several projects -one of them is MVC- to IIS7.
When the MVC project is published and some views are being accessed I get the following error:
Could not load file or assembly 'NHibernate.Mapping.Attributes, Version=3.2.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4' or one of its dependencies. The system cannot find the file specified.
This happens because the view I'm trying to access is calling a function in another project in that solution and needs this library!
but this library is not contained in the MVC project!
I was able to solve it by referencing that DLL in the MVC project! but I need another solution, because it doesn't make sense to reference every single DLL in all the other projects!
I'm using the "Web Deploy" publish option for the MVC project, is there another way that I can publish it and make it work without referencing all the DLLs from all the other projects?
There is an article on how to deploy assemblies that are not referenced by your MVC project directly:
http://haacked.com/archive/2011/05/25/bin-deploying-asp-net-mvc-3.aspx
Basically you need to create a folder in your project called _bin_deployableAssemblies and add your assemblies to it. They will get deployed with the website.
Here is a quote from the article:
So here’s the kicker. If you’re building a web application, and you
need an assembly deployed but don’t want it referenced and don’t want
it checked into the bin directory, you can simply add this folder
yourself and put your own assemblies in here.

MaxLength Attribute in EF4.3.1

The type 'System.ComponentModel.DataAnnotations.MaxLengthAttribute'
exists in both
[path...]\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll
and
'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll'
Now, I have read on msdn that its safe to exclude the EntityFramework reference (which was added through the nuget package). However, when I do that, I cant create a DBContext properly as the DbModelBuilder class lives in the EntityFramework dll. Also, some other critical classes are missing when I remove the EntityFramework refference so this is old and irrelevant solution now.
Update (disambiguation): Both System.ComponentModel.DataAnnotations.dll and EntityFramework.dll include System.ComponentModel.DataAnnotations.MaxLengthAttribute. The problem is that each dll also includes other classes that are critical to EF code-first design. For example:
EntityFramework.dll:
- System.Data.Entity.DbModelBuilder
System.ComponentModel.DataAnnotations.dll:
- System.ComponentModel.DataAnnotations.RegularExpressionAttribute
Add this statement to top of your class
using System.ComponentModel.DataAnnotations;
System.ComponentModel.DataAnnotations namespace is distibuted across the EntityFramework.dll and System.ComponontModel.DataAnnotations.dll. So you need to add a reference to both of that in your project to make use of DataAnnotations.
The MaxLenth attribute is present in EntityFramework.dll. So make sure you have that reference to that dll present in your project references section.
EDIT : As of .NET framework 4.5, this namespace is moved to the System.ComponentModel.DataAnnotations.dll. So If you use .NET Framework 4.5 with Entity Framework 4.3.1 or less, You will run in to this conflict. The solution is to switch to Entity framework 1.50 beta 1/ 2 release if you want to stick with .NET 4.5 or downgrade to .NET 4 to use EntityFramework 4.3.1.
From the msdn documentations.
Starting with Entity Framework 5.0 Beta 1, the EntityFramework.dll
does not contain definitions for data annotations. These definitions
were moved to System.ComponentModel.DataAnnotations.dll and are
defined in the System.ComponentModel.DataAnnotations.Schema namespace.
I didn't have the option of upgrading the project to use EF5, or downgrading the build machine to .Net Framework 4.
There is a way to solve this though! It turns out when you install Visual Studio 2012 it adds the following folder (amongst others).
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
In VS if you open a project targetting 4.0 and look at the properties of your reference to System.ComponentModel.DataAnnotations you'll see that the path is pointing to the above location, rather than the GAC.
This folder contains the original Framework 4.0 assemblies. If they're present on the machine then MSBuild etc., upon building a project targeting 4.0 will reference these rather than the modified ones that 4.5 puts into the GAC.
In our case this meant we could fix the problem by copying that folder from a dev machine with VS installed up to our build server in the same location. (N.b. We only needed to copy this folder, there was no need to install VS on the build server).
More info here: http://marcgravell.blogspot.co.uk/2012/09/iterator-blocks-missing-methods-and-net.html
Hope this helps someone else!

Installing a modified version of ASP.NET MVC

I have made a relatively simple change to the MVC 1.0 source code to correct a bug in the DropDownList HtmlHelper (discussed in another question).
I was able to rebuild the MVC solution, ran all the tests, including the one I hadd added to show the bug I was fixing, and built a new DLL.
But how do I use that?
I tried moving the new MVC dll into the project and changing all the project references to point to it, but when I try to run the MVC website, it's throwing an exception saying there's a conflict between my version of MVC and the MVC in the GAC.
I tried to use gacutil.exe to remove the one from the GAC, but it says it can't do that because it would make another program (the Microsoft Installer? fail.
What do I have to do to install my newly built version in place of the "official" version?
If you want to remove it from the GAC without uninstalling MVC (which includes the Visual Studio templates you probably need), you need to do this in the registry:
Find registry key HKEY_CLASSES_ROOT\Installer\Assemblies\Global and look for key starting with System.Web.Mvc – delete it
Now you should be able to use gacutil.exe to remove System.Web.Mvc from the GAC, and you should be able to use your version from your /bin folder.
How about changing the version number when you compile the modified MVC framework?

Resources