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
- public class Artist
- {
- [ScaffoldColumn(false)]
- public int ArtistId
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "Name")]
- public string Name
- {
- get;
- set;
- }
- public virtual ICollection < Album > Albums
- {
- get;
- set;
- }
- }
Album.cs
- public class Album
- {
- [ScaffoldColumn(false)]
- public int AlbumId
- {
- get;
- set;
- }
- [Required]
- public int GenreId
- {
- get;
- set;
- }
- public int ArtistId
- {
- get;
- set;
- }
- public string Title
- {
- get;
- set;
- }
- [Range(1, 500)]
- public decimal Price
- {
- get;
- set;
- }
- public string AlbumArtURL
- {
- get;
- set;
- }
-
- public virtual Artist Artist
- {
- get;
- set;
- }
- }
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
- public static class ArtistAlbumData
- {
- public static void Initialize(IServiceProvider serviceProvider)
- {
- var context = serviceProvider.GetService < ApplicationDbContext > ();
- context.Database.Migrate();
- if (context.Artist.Any())
- {
- var artist = context.Artist.Add(new Artist
- {
- ArtistId = 1, Name = "Aerosmith"
- }).Entity;
- var dickens = context.Artist.Add(new Artist
- {
- ArtistId = 2, Name = "Alanis Morissette"
- }).Entity;
- var cervantes = context.Artist.Add(new Artist
- {
- ArtistId = 1, Name = "Alice In Chains"
- }).Entity;
- context.Album.AddRange(new Album()
- {
- AlbumId = 1,
- GenreId = 1,
- ArtistId = 1,
- Title = "For Those About To Rock We Salute You",
- Price = 8.99 M,
- AlbumArtURL = "/Content/Images/placeholder.gif"
- }, new Album()
- {
- AlbumId = 2,
- GenreId = 1,
- ArtistId = 1,
- Title = "Let There Be Rock",
- Price = 8.99 M,
- AlbumArtURL = "/Content/Images/placeholder.gif"
- }, new Album()
- {
- AlbumId = 3,
- GenreId = 1,
- ArtistId = 100,
- Title = "Greatest Hits",
- Price = 8.99 M,
- AlbumArtURL = "/Content/Images/placeholder.gif"
- }, new Album()
- {
- AlbumId = 4,
- GenreId = 1,
- ArtistId = 102,
- Title = "Misplaced Childhood",
- Price = 8.99 M,
- AlbumArtURL = "/Content/Images/placeholder.gif"
- });
- context.SaveChanges();
- }
- }
Now open Startup.Cs and add this line of code at the end of Configure method.
- 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
- <ul class="nav navbar-nav">
- <li><a asp-controller="Home" asp-action="Index">Home</a></li>
- <li><a asp-controller="Home" asp-action="About">About</a></li>
- <li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
- <li><a asp-controller="Artists" asp-action="Index">Artists</a></li>
- <li><a asp-controller="Albums" asp-action="Index">Albums</a></li>
- </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: