Getting Started ASP.NET 5 Web App Using Visual Studio 2015

Getting Started 

  • Start Visual Studio 2015
  • From the file menu, select New, then Project
  • Select Web template
  • Select .NET Framework 4.6.1
  • Select ASP.NET Web Application
  • Enter Name
  • Browse save location
  • Click OK

 

Select Web Application from ASP.NET 5 Templates, Do not change the authentication method and click OK.


This is the complete project structure of ASP.NET 5.


Let’s do some briefing about the project structure.

  • appsettings.json - This file allows you to add additional information like connection string values.

  • project.json – It defines .NET execution environment (DNX) project. It is the project.json file that contains all the information that DNX needs to run and package your project.

  • Startup.cs – This class is the entry point of application.

This is the default running screenshot:



Let’s add some model classes in Model directory:

Artist.cs

  1. public class Artist  
  2. {  
  3.     [ScaffoldColumn(false)]  
  4.     public int ArtistId  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     [Required]  
  10.     [Display(Name = "Name")]  
  11.     public string Name  
  12.     {  
  13.         get;  
  14.         set;  
  15.     }  
  16.     public virtual ICollection < Album > Albums  
  17.     {  
  18.         get;  
  19.         set;  
  20.     }  
  21.  

Album.cs

  1. public class Album  
  2. {  
  3.     [ScaffoldColumn(false)]  
  4.     public int AlbumId  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     [Required]  
  10.     public int GenreId  
  11.     {  
  12.         get;  
  13.         set;  
  14.     }  
  15.     public int ArtistId  
  16.     {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public string Title  
  21.     {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     [Range(1, 500)]  
  26.     public decimal Price  
  27.     {  
  28.         get;  
  29.         set;  
  30.     }  
  31.     public string AlbumArtURL  
  32.     {  
  33.         get;  
  34.         set;  
  35.     }  
  36.     // Navigation property    
  37.     public virtual Artist Artist  
  38.     {  
  39.         get;  
  40.         set;  
  41.     }  
  42. }   

Now add new scaffolded item and select model class and data context class.

 

Select data class and context class and click Add. 

Now add one more scaffolded item.


As you can see controller classes has been added on controller’s folder and views are added in views folder.


Now add a new class in model folder

 
  1. public static class ArtistAlbumData  
  2. {  
  3.     public static void Initialize(IServiceProvider serviceProvider)  
  4.     {  
  5.         var context = serviceProvider.GetService < ApplicationDbContext > ();  
  6.         context.Database.Migrate();  
  7.         if (context.Artist.Any())  
  8.         {  
  9.             var artist = context.Artist.Add(new Artist  
  10.             {  
  11.                 ArtistId = 1, Name = "Aerosmith"  
  12.             }).Entity;  
  13.             var dickens = context.Artist.Add(new Artist  
  14.             {  
  15.                 ArtistId = 2, Name = "Alanis Morissette"  
  16.             }).Entity;  
  17.             var cervantes = context.Artist.Add(new Artist  
  18.             {  
  19.                 ArtistId = 1, Name = "Alice In Chains"  
  20.             }).Entity;  
  21.             context.Album.AddRange(new Album()  
  22.             {  
  23.                 AlbumId = 1,  
  24.                     GenreId = 1,  
  25.                     ArtistId = 1,  
  26.                     Title = "For Those About To Rock We Salute You",  
  27.                     Price = 8.99 M,  
  28.                     AlbumArtURL = "/Content/Images/placeholder.gif"  
  29.             }, new Album()  
  30.             {  
  31.                 AlbumId = 2,  
  32.                     GenreId = 1,  
  33.                     ArtistId = 1,  
  34.                     Title = "Let There Be Rock",  
  35.                     Price = 8.99 M,  
  36.                     AlbumArtURL = "/Content/Images/placeholder.gif"  
  37.             }, new Album()  
  38.             {  
  39.                 AlbumId = 3,  
  40.                     GenreId = 1,  
  41.                     ArtistId = 100,  
  42.                     Title = "Greatest Hits",  
  43.                     Price = 8.99 M,  
  44.                     AlbumArtURL = "/Content/Images/placeholder.gif"  
  45.             }, new Album()  
  46.             {  
  47.                 AlbumId = 4,  
  48.                     GenreId = 1,  
  49.                     ArtistId = 102,  
  50.                     Title = "Misplaced Childhood",  
  51.                     Price = 8.99 M,  
  52.                     AlbumArtURL = "/Content/Images/placeholder.gif"  
  53.             });  
  54.             context.SaveChanges();  
  55.         }  
  56.     }  
Now open Startup.Cs and add this line of code at the end of Configure method. 
  1. ArtistAlbumData.Initialize(app.ApplicationServices);   

Now, let’s add Data Migration commands to create database.

Firstly, copy your project physical url and open Command Prompt and run the following commands.


 

As you can see two classes are added in Migrations folder.


You can see newly created database files in SQL Server Object Explorer.

 

Now add navigation for redirect to Artist and Album pages in Views/Shared/_Layout.chtml
  1. <ul class="nav navbar-nav">  
  2.     <li><a asp-controller="Home" asp-action="Index">Home</a></li>  
  3.     <li><a asp-controller="Home" asp-action="About">About</a></li>  
  4.     <li><a asp-controller="Home" asp-action="Contact">Contact</a></li>  
  5.     <li><a asp-controller="Artists" asp-action="Index">Artists</a></li>  
  6.     <li><a asp-controller="Albums" asp-action="Index">Albums</a></li>  
  7. </ul>  
Now build the application. 

Right click on Solutions Explorer and click on View and then click on View On Browser.


Now click on Artists navigation tab from Header.


Click Create


Once you click Create button, as you can see one artist has been added in your list. Here you can edit, details and delete also. 

Summary 

In this article we have learned how to get started with ASP.NET 5 and some basic things. If you havea  question then post a comment in the C# Corner comment section.

Read more articles on ASP.NET:


Similar Articles