Quick Start To Configure ASP.NET Core MVC

Microsoft recently released its next generation platform ASP.NET Core 1.0. ASP.NET Core is a significant redesign of ASP.NET. ASP.NET Core is an open source, lightweight and cross platform Application development platform. ASP.NET Core apps can run on .NET Core or on the full .NET Framework. You can develop and run your ASP.NET Core apps cross-platform on Windows, Mac and Linux. 
In the previous version, Web API and MVC are almost the same (except for one or two differences). Shailendra Chauhan explained meaningful differences between MVC and Web.API in his blog dotnet-tricks. Until now, in MVC5, we were facing issues with Assemblies --  Web API belongs to System.NET.HTTP assembly and MVC belongs to System.Web.MVC. In ASP.NET Core, Microsoft makes it a common assembly Microsoft.ASEPNETCore.MVC for both. In this post, we are going to cover the basic configuration to add MVC in ASP.NET Core project manually.

Let's begin,

In Visual Studio, create a new project (File > New > Project) and select ASP.NET Core Web Application (.NET Core).
ASP.NET Core Web Application
Enter a name of the Application and click OK.

As we are configuring ASP.NET Core MVC from scratch, we need to select an empty template to create a blank project structure.

blank project structure
Visual Studio will automatically generate an empty project structure and redirect the user to Welcome screen.


By default, empty template project will include only required components. The new project structure is different than previously and includes new files like project.json, Startup.cs, wwwroot, global.json and Program.cs.
Just like previous MVC versions, to set up MVC in a project, we need to create Controllers, Models and Views folders.

It's not enough yet! In ASP.NET Core Application, we need to register MVC middleware. First though, you need to add the correct NuGet packages. Open your project.JSON file and add the following dependencies:
  • Microsoft.ASPNETCore.MVC
Search "dependencies" in project.JSON file and Add Microsoft.ASPNETCore.MVC NuGet package references with the release version. Project.JSON file will look like:
  1. "dependencies": {  
  2. "Microsoft.NETCore.App": {  
  3. "version": "1.0.0",  
  4. "type": "platform"  
  5. },  
  6. "Microsoft.AspNetCore.Diagnostics": "1.0.0",  
  7. "Microsoft.AspNetCore.Mvc":"1.0.0",  
  8. "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",  
  9. "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",  
  10. "Microsoft.Extensions.Logging.Console": "1.0.0"  
  11. }  
After adding MVC in project.JSON file, Visual Studio will automatically start restoring the package in the project. References section will look like:
References section

Configure the Application Pipeline

In ASP.NET Core, you can compose your request pipeline, using Middleware. ASP.NET Core middleware performs an Asynchronous logic on an HTTPContext and then either invokes the next middleware in the sequence or terminates the request directly.
In Startup.cs class, ConfigureServices method defines the Services, used by your app (such as the ASP.NET MVC Core framework, Entity Framework Core, Identity, etc.). Add MVC extension method, which will add MVC framework in the project.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.    services.AddMvc();  
  4. }  
Configure defines the middleware in the request. By default, ASP.NET Core doesn't provide any pre-configured route mapping mechanism for MVC. MVC extension method will be used to map Route in Application. 
  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  2. {  
  3.     loggerFactory.AddConsole();  
  5.     if (env.IsDevelopment())  
  6.     {  
  7.         app.UseDeveloperExceptionPage();  
  8.     }  
  10.     app.UseMvc(m =>  
  11.     {  
  12.         m.MapRoute(  
  13.             name: "default",  
  14.             template: "{controller}/{action}/{id?}",  
  15.             defaults: new { controller = "Home", action = "Index" });  
  16.     });  
  17. }  

Add Controller

In Visual Studio, right click on "Controllers" folder. Select MVC Controller class and name it "HomeController".
Visual Studio will create HomeController with default Index action. Next, we will define and initialize the List<string>(). Add some fruit names in the list and pass it in to the return View() statement.
  1. public IActionResult Index()  
  2. {  
  3.     List<string> fruitList = new List<string>()  
  4.     { "Mango",  
  5.         "Apple",  
  6.         "Apricot",  
  7.         "Banana",  
  8.         "Grapes"  
  9.     };  
  11.     return View(fruitList);  
  12. }  

Add View

In Visual Studio, right click "Views" folder, add new folder "Home" (make sure name must be matched with Controller name). Add new MVC view Page with the name "Index" in "Home" folder.
As we are returning List<string>() from controller, declare List<string>() as a @model, so we can loop through it and display the fruit names.
  1. @model List<string>  
  3. <h3>Fruit List</h3>  
  4. <ul>  
  5.     @foreach (var fruit in Model)  
  6.     {  
  7.         <li>@(fruit)</li>  
  8.     }  
  9. </ul>  

Run it!


You can see the fruit names, as we had returned in the controller.


In this article, we discussed how to setup ASP.NET Core MVC from scratch. You can also setup ASP.NET Core MVC by selecting Web Application, instead of empty template, while creating the project.
  • https://docs.asp.net/en/latest/tutorials/first-mvc-app/
  • http://www.asp.net/core
  • https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.html
  • https://docs.asp.net/en/latest/tutorials/first-web-api.html 
Original link.

Up Next
    Ebook Download
    View all
    View all