English 中文(简体)
Nuget Package Management
  • 时间:2024-12-22

ASP.NET MVC - NuGet Package Management


Previous Page Next Page  

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 assembpes 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 apppcations, WPF apppcations, Azure apppcations, any types of apppcation.

Package Management

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

For example, assembpes you want to use might be mock, for mock object unit testing, or NHibernate for data access, and components you use when building your apppcation. 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, pke mock and NHibernate, and Microsoft packages pke 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 pnk. 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 earper and you just need to say Add Package. NuGet knows where to download the latest version, it knows how to extract it, how to estabpsh 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-cpck 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 cpck ‘Install’ button. It will open the Preview dialog.

Preview Dialog

Step 4 − Cpck Ok to continue.

Preview Dialog Cpck Ok

Step 5 − Cpck 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