Introduction
This article mostly focused on ASP.NET core project structure and how to create a basic MVC application. Please clear your MVC, MVVM and IOC concepts before reading this article.
Setup Machine
- Install VS2015 with Update 3
- DotNetCore.1.0.0-VS2015Tools.Preview2.0.1
Download setups from Microsoft website: http://www.asp.net/
Install the above software. After successful installation, open the VS2015.
Create a New Project
<img alt="Project Template" src="http://www.codeproject.com/KB/Articles/1118616/Project_Template.PNG" style="width: 500px; height: 165px;" />
Open VS2015, select the .NET Core template and click ok. Then a new window comes up, select the empty template and click ok.
<img alt="" src="http://www.codeproject.com/KB/Articles/1118616/Empty_Project_Template.PNG" style="width: 500px; height: 363px;" />
The new project structure is completely different from the existing MVC 4.5 project structure. Check the below image:
<img alt="Project Structure" src="http://www.codeproject.com/KB/Articles/1118616/ProjectStructure.PNG" style="width: 447px; height: 552px;" />
Key points are as listed below:
- No global.asax file.
- No Controllers, Views folder, Roue Config, etc.
- Following are some new files added:
- Program.cs: Starting point of your application. This class builds your webhost, invokes the startup class, etc. It performs chain of responsibilities.
- Startup.cs: Do all the application initialization, dependency injection, MVC settings, routing settings, etc. is configured in this file.
- Project.json: Store the application framework, nuget package information(dependencies), publish options, etc.
- Global.json: Points to the application folder structure. e.g. "projects": [ "src"]
Required Nuget Packages
Add the following nuget packages to your application:
- "Microsoft.AspNetCore.Mvc": "1.0.0",
- "Microsoft.AspNetCore.StaticFiles": "1.0.0",
- "Microsoft.Framework.Configuration": "1.0.0-beta8",
- "Microsoft.Framework.Configuration.Json": "1.0.0-beta8"
Project.Json
will be updated after the installation of these nuget packages.
Setup Code
Some features of .NET Core are as follows:
- InBuilt IOC Container
- Logging Mechanism
We are going to develop an application to display in memory customers. Overview of application structure:
- Domain Folder: Stores
customer
object
- Repository Folder: Displays in memory
customer
- Service Folder: It will call
customer
repository to get all customers
. The customer
repository is injected in a constructor.
- Controllers Folder: Customer Controller is created inside it.
Customer
Service is injected in the constructor.
- Views: Inside view folder,
customer
folder is created with Index.cshtml file.
- ViewModel: It stores the
customer
view model. We are using MVVM pattern.
Please see the below image for more details:
<img alt="" src="http://www.codeproject.com/KB/Articles/1118616/project-structure.PNG" style="width: 376px; height: 452px;" />
Configure MVC in Startup.cs File
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<ICustomerRepository, CustomerRepository>();
services.AddScoped<ICustomerAppService, CustomerAppService>();
}
In the above code snippet, I am adding MVC and registering the required components in the inbuilt ioc container.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
app.UseMvc(configureRoutes);
}
private void configureRoutes(IRouteBuilder routeBuilder)
{
routeBuilder.MapRoute("Default", "{controller=Customer}/{action=Index}/{id?}");
}
In the above code, MVC is used and the route is configured.
This is the basic setup we need to perform in .NET Core for MVC application. Download the source code for complete overview of an application.