In this article, we will learn how to publish a reusable Blazor component to nuget gallery. We will use Visual Studio 2017 to build and create the nuget package.
Please refer to my previous article BlazorGrid – A Reusable Grid Component For Blazor to create the BlazorGrid component, which we will publish in this tutorial.
Get the source code for BlazorGrid at GitHub.
Before packaging the component as a nuget package, we need to specify certain information as the metadata for the component.
Right click on BlazorGridComponent project >> select Properties. It will open a property window for the project. Select Package from the left side menu and furnish the details as explained below:
Refer to the image below for a better understanding.
Once you have configured the project properties, change the project configuration from Debug to Release. Rebuild the application to update the DLL files.
Right click on BlazorGridComponent project >> select Pack. It will generate a nuget package file with extension .nupkg. The name of package will be in format of Package id.Package version.nupkg
. Hence in this case the name is BlazorGrid.1.0.0.nupkg
. This file will be located at \BlazorGridComponent\bin\Release
folder.
The next step is to publish the package to nuget.org. We need to have an API key to publish our package.
We need a nuget API key in order to push a package to nuget.org.
To generate the API key follow the steps mentioned below:
Refer to the image below:
It will create your new API Key. This key is required to publish the nuget package. Copy the key using the Copy button at the bottom. Refer to the image below:
Important Note
Make sure to copy your new API key at this point using the Copy button. You won’t be able to copy the key once you leave this web page.
Once you obtained the API key, navigate to the folder where the .nupkg
file is located which is \BlazorGridComponent\bin\Release
in this case. Open command prompt or PowerShell window. Run the following command to publish the package:
nuget push BlazorGrid.1.0.0.nupkg oy2klaw3g67amlbxp3qwkzr4exypy3bhxy6w6x6 -Source https://api.nuget.org/v3/index.json
This command takes the package name and the API key value as input and publish the package to nuget.org. Make sure to put in your own package name and API key value before executing this command. If the package is pushed successfully, you will get a success message on the window.
Nuget.org will take few minutes to validate the package. Upon successful validation, the package will be indexed on the website and you will receive an email confirmation about the same. You can find all your nuget packages in the Manage packages
section under your account name at nuget.org.
The BlazorGrid component that we have published here is available on nuget gallery at https://www.nuget.org/packages/BlazorGrid/.
If you face issues during package push and get errors in console, try following troubleshooting methods
To install the package in your Blazor project, run the following command in package manger console:
Install-Package BlazorGrid
After you have installed the package, add the following line in the _ViewImports.cshtml
file
@addTagHelper *,BlazorGridComponent
This will allow us to use the BlazorGrid package in our Blazor project.
The <BlazorGrid>
component accepts following parameters
The syntax for BlazorGrid is shown below:
<BlazorGrid Items="objectList" PageSize="a positive integer"> <GridHeader> <th>header 1</th> <th>header 2</th> </GridHeader> <GridRow> <td>@context.objProperty1</td> <td>@context.objProperty2</td> </GridRow> </BlazorGrid>
We need to provide the object list and the page size for the grid. The header for grid should be specified in <GridHeader>
parameter. We will use <GridRow>
to specify the row items for the grid. For passing the values to <GridRow>
, we will use the implicit parameter “context”, provided by Blazor framework.
We learned how to create and publish a reusable Blazor component as nuget package. We also troubleshoot few common push errors for nuget packages. The BlazorGrid can be used to display a set of data in grid and has support for client-side pagination. We learned how to install and use BlazorGrid in a Blazor project.
Get my book Blazor Quick Start Guide to learn more about Blazor.
You can also read this article at C# Corner.
Introduction Blazor is a .NET web framework that allows us to create client-side applications using…
Introduction In this article, we are going to create a sudoku solver with the help…
Introduction In this article, we will learn how to implement Azure serverless with Blazor web…
Introduction Angular is an open-source framework that allows us to create applications for multiple platforms…
Introduction In this article, we will create an optical character recognition (OCR) application using Angular…
Introduction In this article, we will create an optical character recognition (OCR) application using Blazor…
View Comments
very nice article, just some quick notes:
when trying to push the nuget package then:
1. open the cmd as administrator
2. run command : nuget update -self
this command will upgrade nuget client to latest, otherwise you may experience the error: A client version '4.1.0' or higher is required to be able to push packages.
...and inside project properties of BlazorGridComponent, remove value from license url, instead click radio button license file and chose the license (LICENSE) file at the root of the project.
At last make sure to remove project reference from E2ETestApp