Create and Push NuGet Package in Visual Studio 2013

In this article we will see how to create a NuGet Package after each build and push the package to NuGet in Visual Studio 2013.

NuGet

NuGet is a Visual Studio extension that makes it easy to pull in libraries, components and most importantly their configuration into your Visual Studio project. This is a tool that is installed with MVC 3 and it is used to bring in various components to make developing on MVC easier. These components are called NuGet Packages and they can include .NET assemblies, JavaScript files, HTML/Razor files, CSS files, images and even files that can add configuration to your project's web.config. The goal of NuGet is to make it super-easy to bring in or update a component in your existing projects.

More info: Using NuGet Packages.

First of all let's make an MVC project.

Getting Started

  • Create a new Project. Open Visual Studio 2013.
  • Go to "File" -> "New" -> "Project".
  • Select "Web" in the installed templates.
  • Select "MVC".
  • Enter the Name and choose the location.
  • Click "OK"

In this small sample I am building CRUD operations using Entity Framework.

Model classes:

  1. public class Friend  
  2. {  
  3.     [Key]  
  4.     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]  
  5.     public int FriendId { getset; }  
  6.     public string FirstName { getset; }  
  7.     public string LastName { getset; }  
  8.     public string Address { getset; }  
  9.     public string City { getset; }  
  10.     public string PostalCode { getset; }  
  11.     public string Country { getset; }  
  12.     public string Notes { getset; }  
  13. }  
  14. public class FriendsContext : DbContext  
  15. {  
  16.     public FriendsContext()  
  17.         : base("name=DefaultConnection")  
  18.     {  
  19.         base.Configuration.ProxyCreationEnabled = false;  
  20.     }  
  21.   
  22.     public DbSet<Friend> Friends { getset; }  
  23. }  
Controller class:
  1. public class FriendsController : Controller  
  2. {  
  3.     private FriendsContext db = new FriendsContext();  
  4.   
  5.     // GET: Friends  
  6.     public async Task<ActionResult> Index()  
  7.     {  
  8.         return View(await db.Friends.ToListAsync());  
  9.     }  
  10.   
  11.     // GET: Friends/Details/5  
  12.     public async Task<ActionResult> Details(int? id)  
  13.     {  
  14.         if (id == null)  
  15.         {  
  16.             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  17.         }  
  18.         Friend friend = await db.Friends.FindAsync(id);  
  19.         if (friend == null)  
  20.         {  
  21.             return HttpNotFound();  
  22.         }  
  23.         return View(friend);  
  24.     }  
  25.   
  26.     // GET: Friends/Create  
  27.     public ActionResult Create()  
  28.     {  
  29.         return View();  
  30.     }  
  31.   
  32.     // POST: Friends/Create  
  33.     // To protect from overposting attacks, please enable the specific properties you want to bind to, for   
  34.     // more details see http://go.microsoft.com/fwlink/?LinkId=317598.  
  35.     [HttpPost]  
  36.     [ValidateAntiForgeryToken]  
  37.     public async Task<ActionResult> Create([Bind(Include = "FriendId,FirstName,LastName,Address,City,PostalCode,Country,Notes")] Friend friend)  
  38.     {  
  39.         if (ModelState.IsValid)  
  40.         {  
  41.             db.Friends.Add(friend);  
  42.             await db.SaveChangesAsync();  
  43.             return RedirectToAction("Index");  
  44.         }  
  45.   
  46.         return View(friend);  
  47.     }  
  48.   
  49.     // GET: Friends/Edit/5  
  50.     public async Task<ActionResult> Edit(int? id)  
  51.     {  
  52.         if (id == null)  
  53.         {  
  54.             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  55.         }  
  56.         Friend friend = await db.Friends.FindAsync(id);  
  57.         if (friend == null)  
  58.         {  
  59.             return HttpNotFound();  
  60.         }  
  61.         return View(friend);  
  62.     }  
  63.   
  64.     // POST: Friends/Edit/5  
  65.     // To protect from overposting attacks, please enable the specific properties you want to bind to, for   
  66.     // more details see http://go.microsoft.com/fwlink/?LinkId=317598.  
  67.     [HttpPost]  
  68.     [ValidateAntiForgeryToken]  
  69.     public async Task<ActionResult> Edit([Bind(Include = "FriendId,FirstName,LastName,Address,City,PostalCode,Country,Notes")] Friend friend)  
  70.     {  
  71.         if (ModelState.IsValid)  
  72.         {  
  73.             db.Entry(friend).State = EntityState.Modified;  
  74.             await db.SaveChangesAsync();  
  75.             return RedirectToAction("Index");  
  76.         }  
  77.         return View(friend);  
  78.     }  
  79.   
  80.     // GET: Friends/Delete/5  
  81.     public async Task<ActionResult> Delete(int? id)  
  82.     {  
  83.         if (id == null)  
  84.         {  
  85.             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  86.         }  
  87.         Friend friend = await db.Friends.FindAsync(id);  
  88.         if (friend == null)  
  89.         {  
  90.             return HttpNotFound();  
  91.         }  
  92.         return View(friend);  
  93.     }  
  94.   
  95.     // POST: Friends/Delete/5  
  96.     [HttpPost, ActionName("Delete")]  
  97.     [ValidateAntiForgeryToken]  
  98.     public async Task<ActionResult> DeleteConfirmed(int id)  
  99.     {  
  100.         Friend friend = await db.Friends.FindAsync(id);  
  101.         db.Friends.Remove(friend);  
  102.         await db.SaveChangesAsync();  
  103.         return RedirectToAction("Index");  
  104.     }  
  105.   
  106.     protected override void Dispose(bool disposing)  
  107.     {  
  108.         if (disposing)  
  109.         {  
  110.             db.Dispose();  
  111.         }  
  112.         base.Dispose(disposing);  
  113.     }  
  114. }  
Friends View:
  1. @model IEnumerable<RajNugetPackage.Models.Friend>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>Index</h2>  
  9.   
  10. <p>  
  11.     @Html.ActionLink("Create New""Create")  
  12. </p>  
  13. <table class="table">  
  14.     <tr>  
  15.         <th>  
  16.             @Html.DisplayNameFor(model => model.FirstName)  
  17.         </th>  
  18.         <th>  
  19.             @Html.DisplayNameFor(model => model.LastName)  
  20.         </th>  
  21.         <th>  
  22.             @Html.DisplayNameFor(model => model.Address)  
  23.         </th>  
  24.         <th>  
  25.             @Html.DisplayNameFor(model => model.City)  
  26.         </th>  
  27.         <th>  
  28.             @Html.DisplayNameFor(model => model.PostalCode)  
  29.         </th>  
  30.         <th>  
  31.             @Html.DisplayNameFor(model => model.Country)  
  32.         </th>  
  33.         <th>  
  34.             @Html.DisplayNameFor(model => model.Notes)  
  35.         </th>  
  36.         <th></th>  
  37.     </tr>  
  38.   
  39. @foreach (var item in Model) {  
  40.     <tr>  
  41.         <td>  
  42.             @Html.DisplayFor(modelItem => item.FirstName)  
  43.         </td>  
  44.         <td>  
  45.             @Html.DisplayFor(modelItem => item.LastName)  
  46.         </td>  
  47.         <td>  
  48.             @Html.DisplayFor(modelItem => item.Address)  
  49.         </td>  
  50.         <td>  
  51.             @Html.DisplayFor(modelItem => item.City)  
  52.         </td>  
  53.         <td>  
  54.             @Html.DisplayFor(modelItem => item.PostalCode)  
  55.         </td>  
  56.         <td>  
  57.             @Html.DisplayFor(modelItem => item.Country)  
  58.         </td>  
  59.         <td>  
  60.             @Html.DisplayFor(modelItem => item.Notes)  
  61.         </td>  
  62.         <td>  
  63.             @Html.ActionLink("Edit""Edit"new { id=item.FriendId }) |  
  64.             @Html.ActionLink("Details""Details"new { id=item.FriendId }) |  
  65.             @Html.ActionLink("Delete""Delete"new { id=item.FriendId })  
  66.         </td>  
  67.     </tr>  
  68. }  
  69.   
  70. </table>  
Output:

index
                                                                                    Image 1

Add The NuGet Package to you Project

 

  • Right-click on the project's References and click Manage NuGet Packages.

    Manage NuGet Packages
                                                    Image 2

  • In the NuGet Package Manager, select online from the menu and search “New NuGet Package” and click Install.

    New NuGet Package
                                                                               Image 3

  • Now you can see that the _CreateNewNuGetPackage folder was added to your project that contains the files used to create a NuGet package from your project after each build.

    reference
                                        Image 4

  • Now build your project to generate the package, as you can see the NuGet Package file (.nupkg) was created in the project's output directory.

    NuGet Package
                                                             Image 5

  • If you want to change your package's version number, apiKey, Platform, release note and so on then you can edit the Config.ps1 file.

    apiKey
                                                                Image 6

Push Package to NuGet Gallery

Right-click on RunMeToUploadNuGetPackage under the _CreateNewNuGetPackage folder and click Run.

RunMeToUploadNuGetPackage
                                                         Image 7

If you don't see the Run command then download and install, you can get this functionality by installing the VSCommands Visual Studio extension; otherwise you will need to run the batch file from the Windows/File Explorer.

VSCommands
                                                                  Image 8

run the batch file
                                                         Image 9.

Make sure that when you build the application there is no error that occurrs.

*no error occurred
                                                                              Image 10

You can see your package list on: Nuget after logging in on my packages.

my packages
                                                                  Image 11

You can search online on the Nuget package using the name.

online on nuget package
                                                                           Image 12

Note: You need to provide the NuGet ApiKey that is available in my account when you login into the Nuget website.

NuGet ApiKey
                                                                           Image 13

Conclusion

In this article we have learned how to create a NuGet Package and how to push that package to the NuGet Gallery.

Up Next
    Ebook Download
    View all
    Learn
    View all