Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create a Simple MVC Application using ASP.NET MVC Core 1.0

0.00/5 (No votes)
15 Aug 2016 1  
Step by step guide to create a simple MVC application using ASP.NET Core

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

  1. Install VS2015 with Update 3
  2. 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:

  1. No global.asax file.
  2. No Controllers, Views folder, Roue Config, etc.
  3. Following are some new files added:
    1. Program.cs: Starting point of your application. This class builds your webhost, invokes the startup class, etc. It performs chain of responsibilities.
    2. Startup.cs: Do all the application initialization, dependency injection, MVC settings, routing settings, etc. is configured in this file.
    3. Project.json: Store the application framework, nuget package information(dependencies), publish options, etc.
    4. 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:

  1. InBuilt IOC Container
  2. Logging Mechanism

We are going to develop an application to display in memory customers. Overview of application structure:

  1. Domain Folder: Stores customer object
  2. Repository Folder: Displays in memory customer
  3. Service Folder: It will call customer repository to get all customers. The customer repository is injected in a constructor.
  4. Controllers Folder: Customer Controller is created inside it. Customer Service is injected in the constructor.
  5. Views: Inside view folder, customer folder is created with Index.cshtml file.
  6. 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.

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
       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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here