How to add System.Web.Mvc version 5 to the project references - asp.net-mvc

When I want to add System.Web.Mvc by right clicking the project and Add>Reference option, there is only version 3.0.0.0 and 4.0.0.0. However, I created the project as MVC 5. On the other hand, trying to add this reference on Nuget Package, there is no reference with the same name. Instead of this, there is another refernce named So:
1) How can add System.Web.Mvc (version 5 or later) reference to my projects including Class Library?
2) What is the difference between System.Web.Mvc and Microsoft.AspNet.Mvc?
Thanks in advance.

System.Web.Mvc Is one of the core namespaces developed by MS,it contains classes and interfaces that support the ASP.NET Model View Controller (MVC) framework for creating Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial view, model binders, and much more. MS Link
Don't get confused with another namespace: Microsoft.Web.Mvcwhich contains classes that support the ASP.NET.MVC framework for creating web applications.
Now back to your first question about Microsoft.AspNet.Mvcthat you can download from Nuget. I think it's the same as System.web.mvc , if you check dll file it creates, you will notice that it has the exact samedll name: System.Web.Mvc.dll. It has the same size, assembly signature, etc.

I experienced this odd issue and had to uninstall/reinstall Microsoft ASP.NET MVC via NuGet Package Manager - for the project in question - before I could add the correct System.Web.Mvc v5.0.

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

The type or namespace name 'IClientValidatable' could not be found - in .NET 4.7

I'm building an ASP.NET MVC app with VS 2017, targeting .NET 4.7, and I'm trying to build my models inside of business library being referenced by the web application. So I'm trying to get many of the same classes that come with an MVC project by default into a brand new class library.
Particularly, I'm getting the following error:
This is very different from the previous times this question was posted:
The type or namespace name 'IClientValidatable' could not be found
The type or namespace name 'IClientValidatable' could not be found (are you missing a using directive or an assembly reference?)
Namely, in that they both are solved simply by including the using statement for System.Web.Mvc where IClientValidatable lives.
But I've definitely already done that... Here's the reference manager for the class library with the reference included available for .NET 4.7
As further proof, here's a side by side example of where I can pull in some classes from System.Web.Mvc, but not IClientValidatable
For reference sake, here's the configuration on the class library itself
So did this class move somewhere? Is it available with .NET 4.7?
So the real question seems to be not where did it go, but why the most recent framework is targeting such an old version.
When trying to add a reference to System.Web.MVC while targeting .NET Framework 4.7, the only available option is MVC v2.0.0.0 which is incredibly old. When seeding a new MVC app, it'll come with v5.2.3.0
Here's the Assembly Explorer with both libraries loaded. Common items will be highlighted in each, but IClientValidatable wasn't added until later.
And here's the VS 2017 Reference Manager where you can add references based on your current framework, showing the old MVC library for the new .NET version.
So the question then becomes....
Q: How can I add a reference that is not available in the list of assemblies for my framework?
A: Same way you'd add any other reference - you can browse to a dll or grab it from nuget.
DLL If you already have a web app, there are good odds you can find the dll in your existing packages directory at something like:
\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll
If you prefer Nuget, some minor annoyances are that nuget package names don't exactly sync up with the assemblies they install, it's not easy to browse or search which assemblies come with which packages, and they libraries and frameworks often come bundled with other things you might not need. All that said, you'll be looking for the package called:
Microsoft.AspNet.Mvc on Nuget
Which will install the following libraries:

Unable to add reference to certain assemblies in VS2015 MVC project

I am relatively new to MVC, so maybe I am missing something. I am using VS 2015 Community Edition, C# 2015 & .NET Framework 4.5.2.
I have an MVC solution with 2 projects: TLM and TLM.Tests. All the .cs files in both projects are happy with the following line of code:
using System.Data;
In fact, one of the .cshtml files has this line:
#using System.Data;
Now I add a 3rd project to the solution, a Class Library called TLM.Helpers, to hold commonly used code. (In ASP.NET web forms, it would have just gone in a .cs file in the App_Code folder.) However, I cannot add a using statement for System.Data to any of the .cs files in this project. In fact, I cannot even add a reference to that DLL in this project, although I see that there is such a reference in the other 2 projects.
That new project does have successful references to System, System.Collections.Generic, and System.Linq.
Other posts for similar issues suggest verifying that the project is being built for the proper Framework version. The Property page for this project does not even offer the choice of Target Framework. It does have dropdowns for "Solution DNX SDK version", which refers to "Version 1.0.0-beta5". I don't know what that refers to.
Any suggestions as to how I can refer to System.Data in this project?
Thanks.
Dan

How to create an ASP.NET MVC class library

I want to create a class library for an MVC 4 web application. Every search I've tried has returned plenty of references that merely mention creating one, or the importance of doing so, but not specifics of how.
My first assumption was a template would be under Web in the Visual Studio New Project dialog, but no. I was unsure if I was to use the Class Library template under Windows, but did.
I want to include things like some data access (e.g., DbContext), but while Intellisense sees the System.Data.Entity namespace, there are no classes available. I guess I need some additional references, but no idea which ones. Looking at the references in my main MVC project, at lot of them are pointing to the Packages folder. I'm unsure if I should be doing the same.
In short, I'm looking for instructions on how to create a class library for MVC in Visual Studio, including the necessary references for EF, Razor and whatever else.
you used the correct template - a simple class library is all you need.
then in the MVC web project just add a reference to the class library project
Use NuGet to add references to the pieces of functionality, like EF and System.Web.MVC, that you need in your class library or libraries.
A data access project to handle persistence and a class library to hold HTML Helpers that you might want to reuse both make some sense. Razor views if you're using the RazorEngine rendering stack can also be interesting to be able to test.
You are right to use the Class Library template in visual studio for your needs. You can add all of the references you need through NuGet (such as Razor, EF, and so on) and by Right clicking on references in the Solution Explorer and picking and choosing what you need.
Remember when using multiple projects that you add references between projects too! (for example your Web App project needs to know about your Data Repository Project)

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!

Resources