ASP.NET MVC - NuGet Package Management



In this chapter, we will talk about NuGet which is a package manager for .NET and Visual Studio. NuGet can be used to find and install packages, that is, software pieces and assemblies and things that you want to use in your project.

NuGet is not a tool that is specific to ASP.NET MVC projects. This is a tool that you can use inside of Visual Studio for console applications, WPF applications, Azure applications, any types of application.

Package Management

NuGet is a package manager, and is responsible for downloading, installing, updating, and configuring software in your system. From the term software we don’t mean end users software like Microsoft Word or Notepad 2, etc. but pieces of software, which you want to use in your project, assembly references.

For example, assemblies you want to use might be mock, for mock object unit testing, or NHibernate for data access, and components you use when building your application. The above-mentioned components are open source software, but some NuGet packages you find are closed source software. Some of the packages you'll find are even produced by Microsoft.

The common theme along all the packages mentioned above, like mock and NHibernate, and Microsoft packages like a preview for the Entity Framework, is that they don't come with Visual Studio by default.

Without NuGet

To install any of these components without NuGet, you will need the following steps.

Components without NuGet

If you want to use one of those components, you need to find the home page for some particular project and look for a download link. Then once the project is downloaded, it's typically in a ZIP format so you will need to extract it.

If you didn't download binaries, then you will first need to build the software and then reference it in your project. And many components at that point still require some configuration to get up and running.

Using NuGet

NuGet replaces all of the steps discussed earlier and you just need to say Add Package. NuGet knows where to download the latest version, it knows how to extract it, how to establish a reference to that component, and even configure it. This leaves you more time to just build the software.

Let’s take a look at a simple example in which we will add support for Entity framework in our ASP.NET MVC project using NuGet.

Step 1 − Install the Entity Framework. Right-click on the project and select NuGet Package Manager → Manage NuGet Packages for Solution…

Select NuGet Package Manager

It will open the NuGet Package Manager.

Step 2 − Search for Entity framework in the search box.

Search for Entity Framework

Step 3 − Select the Entity Framework and click ‘Install’ button. It will open the Preview dialog.

Preview Dialog

Step 4 − Click Ok to continue.

Preview Dialog Click Ok

Step 5 − Click the ‘I Accept’ button to start the installation.

I Accept Installation

Once the Entity Framework is installed you will see the message in out window as shown above.

When you install a package with NuGet, you will see a new packages directory in the same folder as the solution file hosting your project. This package directory contains all the packages that you have installed for any of the projects in that solution.

All Packages in Projects

In other words, NuGet is not downloading packages into a central location, it's storing them on a per solution basis.

Advertisements