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

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

index
Image 1

Add The NuGet Package to you Project

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.