Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
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.
Tips/Råd
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.
Förutsättningar
- The .NET SDK, which provides the
dotnetcommand-line tool. Starting in Visual Studio 2017, the dotnet CLI automatically installs with any .NET or .NET Core related workloads.
Skapa ett projekt
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 consoleUse
dotnet runto test the app. You should see the outputHello, World!.
Lägg till NuGet-paketet Newtonsoft.Json
Använd följande kommando för att installera
Newtonsoft.jsonpaketet:dotnet add package Newtonsoft.JsonAfter 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>
Använda Newtonsoft.Json-API:et i appen
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 runcommand. The output is the JSON representation of theAccountobject 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!
Relaterad video
Find more NuGet videos on Channel 9 and YouTube.
Nästa steg
Learn more about installing and using NuGet packages with the dotnet CLI: