[ASP. net mvc series] about the application of NuGet in VS, mvcnuget

Source: Internet
Author: User
Tags log4net

[ASP. net mvc series] about the application of NuGet in VS, mvcnuget

I. Overview

Before explaining NuGet, let's take a look at an example.

1. Example:

Assume that a system is developed, and the frontend framework is Bootstrap. Because Bootstrap is selected as the frontend framework, we need to introduce relevant Bootstrap files in the project and perform the following operations.

Step 1: Download the Bootstrap File

Enter Bootstrap official website domain name http://v3.bootcss.com/

Enter the domain name to go to the official website

Step 2: select an appropriate version and download the Bootstrap package

Step 3: import the package to the Project

 

The process of introducing the Bootstrap package into the Project seems to be quite troublesome. what's even more frightening is that when the Bootstrap package in the Project needs to be updated, we need to repeat the work, which is very tedious, well, the NuGet Package Manager provided by VS is good.

This helps us solve these problems.

2. What is the Nuget Package Manager?

For. NET and VS, NuGet is A. NET package management system that can easily respond to the application to add, update, and delete External library files and their dependencies.

In addition, we can share packages by creating packages and publishing packages.

From Microsoft's point of view: try as much as possible, and do not expect Microsoft to provide us with every piece of code we need. the number of developers who develop on the NET platform is millions or even tens of millions, and each developer has its unique technology and needs to be solved.

Wait for Microsoft to solve every problem of every developer. It is neither a scale nor a significance. Fortunately, they can solve problems they or their customers encounter through some online Kuai. However

We are faced with three major challenges: discovery, installation and maintenance. Fortunately, NuGet solves such problems well.

2. Add the package to the library

There are many methods to add packages to the library. The common methods are Manage NuGet Package and Package Manager Console. The following describes the two methods based on VS2017 and ASP. net mvc templates.

(1) Manage NuGet Packages

1. Prepare the VS2017 development environment and use the MVC template of VS to create a default project: NuGetDemo

Analysis:

If you are careful, you will find that packages is specified. the config file is actually the configuration file of the NuGet package, because when we choose to create a project based on the MVC template, the third-party library files, such as Bootstrap and Jquery, are introduced by default;

2. Open the NuGet Package Manager

Reference => Manage Nuget packages

 

3. Open the NuGet Package Manager and perform the following operations:

(1) Browse package (2) view the current system installed package (3) update package (4) search the package in the search box

4. view the package details

Take log4net as an example.

Analysis:

Basic Package Information

5. Install

Select the installation version before installing AnyLog. Log4Net.

6. view the installation status

(1) view the installation information output on the console

(2) Check the installation status of the NuGet package

(3) view project references

(4) view the packages. config file

7. Update the package

The NuGet Package Manager not only helps us to install packages, but also helps us maintain packages. We can use the NuGet Package Manager to update packages.

Note: When we update a package, this operation will also update the dependency of the package to ensure that only the dependency compatibility is guaranteed.

In the following example, we have installed Log4net 2.0.3 in the NuGet project. Now, we have updated all of them to the latest version 2.0.8 and select "Update ".

Click OK.

Note: This step updates all Log4net-related content in the application.

View UPDATE results

 

8. Package recovery

The default workflow of NuGet is to submit the package folder to version control. One advantage of this is that you can retrieve the solution from version control to ensure that each package of the build solution can be installed, these packages do not need to be retrieved from other locations.

However, this method also has some disadvantages. The Packages folder is not part of the VS solution. Therefore, developers who manage version control through VS integration need to take an additional step to ensure that the Packages folder can be submitted.

(1) TFS can automatically submit the Packages folder

(2) For a distributed version control system (such as Git), it is not recommended to submit the Packages folder to version control because the distributed version control system is not good at processing binary files, if a large number of packages in the project change, the distributed version control system library will

It becomes very huge.

Fortunately, the package fix feature introduced by NuGet 1.6 is used to solve these problems, so as to support a new workflow. We do not need to submit the Packages folder to the source code for control.

In this process, you need to perform several steps manually: perform a separate step for each project to enable package recovery (in NuGet2.0 ~ 2.6, each developer also needs to configure VS to allow packet recovery)

Note:

Now NuGet is automatically enabled when the Package is restored, but the following two options can be used in VS Package Manager settings to disable the Package recovery function:

A. Allow NuGet to download missing packages;

B. When constructing an application in VS, the system automatically checks the missing package;

The NuGet 2.7 automatic package recovery function is used.

(2) Package Manager Console

The Package Manager Console is a PowerShell-based Console in Visual Studio. It not only provides powerful functions to find and install Packages, but also supports some features not supported by the Manager NuGet Packages dialog box.

1. Start the Console

Tool => NuGet Package Manager => Package Manager Console

2. view the packages installed in the current project

Enter Get -- Package in the console to view the packages installed in the current project.

Expansion:

(1) In earlier versions, you can use the following command to filter packets

Get-Package -ListAvailable -Filter Route

(2) Replace the Get-Package-ListAvailable-Filter command with the following command:

Find-Package [-Id]

 

3. Installation Package

In the console, run Install-Package name

.

Expansion:

(1) PowerShell commands support extended functions, which is equivalent to smart sensing. Enter some characters in the command, and press the Tab key to view the input content for intelligent sensing.

(2) PowerShell supports compound commands, such as transmitting one command pipeline to another.

Run the following command to install a package for each project in the solution.

Get-Project -ALL | Install-Package  log4net

(3) PowerShell also supports dynamic addition of new commands

PowerShell is powerful in that some installed packages can add new commands for shell.

(3) Enable NuGet in other ways

For example, if you add a project name, the core content is still in the (1) and (2) sections above.

4. Package source and project selection

Select the gear on the right of the package source to view the package and perform basic operations on the package.

5. Overview of NuGet Package Manager

(1) Basic interface of NuGe Package Manager

In console operations, there are two main components: Regular and package source.

(2) package source Overview

Expansion:

In the Manage NuGet Packages operation package mode

Example:

The following describes how to add an autofac package based on the above two modes.

Manage NuGet Packages Method

Console Operation Method

(1) Find the NuGet address of the autofac package

(2) Add an autofac package

3. Create a package

Limited space. I will write a special article in the future ......

Four release packages

Limited space. I will write a special article in the future ......

5 References

[01] ASP. NET MVC5 advanced programming (Jon Galloway, Brad Wilson, K. Scott Allen, David Matson, translated by Sun yuanshuai)

Copyright 6

  • Thank you for your reading. If you have any shortcomings, please kindly advise and learn and make progress together.
  • Blog website: http://www.cnblogs.com/wangjiming /.
  • A small number of articles are integrated by reading, referencing, referencing, copying, copying, and pasting. Most of them are original articles.
  • If you like, please recommend it; if you have new ideas, welcome to raise, mailbox: 2016177728@qq.com.
  • This blog can be reposted, but it must be a famous blog source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.