Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
NuGet packages contain compiled binary code that developers make available for other developers to use in their projects. For more information, see What is NuGet. This quickstart describes how to install the popular Newtonsoft.Json NuGet package into a .NET project by using the dotnet add package command.
You refer to installed packages in code with a using <namespace>
directive, where <namespace>
is often the package name. You can then use the package's API in your project.
Hint
Browse nuget.org/packages to find packages you can reuse in your own applications. You can search directly at https://nuget.org, or find and install packages from within Visual Studio. For more information, see Find and evaluate NuGet packages for your project.
Vereiste voorwaarden
- The .NET SDK, which provides the
dotnet
command-line tool. Starting in Visual Studio 2017, the dotnet CLI automatically installs with any .NET or .NET Core related workloads.
Een project maken
You can install NuGet packages into a .NET project. For this walkthrough, create a simple .NET console project by using the dotnet CLI, as follows:
Create a folder named Nuget.Quickstart for the project.
Open a command prompt and switch to the new folder.
Create the project by using the following command:
dotnet new console
Use
dotnet run
to test the app. You should see the outputHello, World!
.
Add the Newtonsoft.Json NuGet package
Gebruik de volgende opdracht om het
Newtonsoft.json
pakket te installeren:dotnet add package Newtonsoft.Json
After the command completes, open the Nuget.Quickstart.csproj file in Visual Studio to see the added NuGet package reference:
<ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> </ItemGroup>
Use the Newtonsoft.Json API in the app
In Visual Studio, open the Program.cs file and add the following line at the top of the file:
using Newtonsoft.Json;
Add the following code to replace the
Console.WriteLine("Hello, World!");
statement:namespace Nuget.Quickstart { public class Account { public string? Name { get; set; } public string? Email { get; set; } public DateTime DOB { get; set; } } internal class Program { static void Main(string[] args) { Account account = new Account { Name = "John Doe", Email = "john@nuget.org", DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc), }; string json = JsonConvert.SerializeObject(account, Formatting.Indented); Console.WriteLine(json); } } }
Save the file, then build and run the app by using the
dotnet run
command. The output is the JSON representation of theAccount
object in the code:{ "Name": "John Doe", "Email": "john@nuget.org", "DOB": "1980-02-20T00:00:00Z" }
Congratulations on installing and using your first NuGet package!
Gerelateerde video
Vind meer NuGet-video's op Channel 9 en YouTube-.
Volgende stappen
Learn more about installing and using NuGet packages with the dotnet CLI: