Click here to Skip to main content
15,891,657 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.40/5 (8 votes)
15 Aug 2016CPOL2 min read 17.7K   156   12   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

Project Template

Open VS2015, select the .NET Core template and click ok. Then a new window comes up, select the empty template and click ok.

The new project structure is completely different from the existing MVC 4.5 project structure. Check the below image:

Project Structure

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:

Configure MVC in Startup.cs File

C#
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.

C#
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Test_256425-Aug-16 10:29
Test_256425-Aug-16 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.