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).
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.
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:
Search "dependencies" in project.JSON file and Add Microsoft.ASPNETCore.MVC NuGet package references with the 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 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.
- 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. MVC extension method will be used 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 "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.
- @model List<string>
-
- <h3>Fruit List</h3>
- <ul>
- @foreach (var fruit in Model)
- {
- <li>@(fruit)</li>
- }
- </ul>
Run it!
"
You can see the fruit names, as we had returned in the controller.
Summary
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.
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