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 previous version, Web API and MVC are almost same(except one or two). Shailendra Chauhan had explained meaningful difference between MVC and Web.API on his blog dotnet-tricks. Until now, in MVC5 we are facing issues with assemblies, Web API belong to System.Net.Http assembly and MVC belongs to System.Web.MVC. But in ASP.NET Core Microsoft makes it common assembly Microsoft.AspNetCore.Mvc for both. In this post we are going to cover 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).


Enter a name of the application and click OK.

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

Visual Studio will automatically generates an empty project structure and redirect user to Welcome screen.

By default, empty template project will include only required components. The new project structure is different than previous and includes new files like project.json, Startup.cs, wwwroot, global.json and Program.cs.

Just like previous MVC versions, to setting 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, so 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 release version. project.json file will look like,

"dependencies": {
"Microsoft.NETCore.App": {
  "version": "1.0.0",
  "type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc":"1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0"
}

After adding MVC in project.json file, Visual Studio will automatically start restoring the package in the project. References section will look like,


Configure the Application Pipeline

In ASP.NET Core you can compose your request pipeline using Middleware. ASP.NET Core middleware performs 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.). AddMvc extension method will add MVC framework in the project.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

Configure defines the middleware in the request. By default ASP.NET Core doesn't provide any pre-configured route mapping mechanism for MVC. UseMvc extension method will use to map Route in application.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc(m =>
    {
        m.MapRoute(
            name: "default",
            template: "{controller}/{action}/{id?}",
            defaults: new { controller = "Home", action = "Index" });
    });
}

 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.
public IActionResult Index()
{
    List<string> fruitList = new List<string>()
    { "Mango",
        "Apple",
        "Apricot",
        "Banana",
        "Grapes"
    };

    return View(fruitList);
}

Add View

In Visual Studio right click on "Views" folder, add new folder "Home" (make sure name must be matched with Controller name). Add new MVC view Page with 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 fruit names.
@model List<string>

<h3>Fruit List</h3>
<ul>
    @foreach (var fruit in Model)
    {
        <li>@(fruit)</li>
    }
</ul>

Run it!

All done! You can see fruit names as we had returned in the controller.

Summary

In this article, we have 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 project.

Reference
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