Introduction:

In this article we are going to explain how to create mvc .net core 2.0 application and add view using in visual studio 2017 with example.

Description:

How to create an MVC ASP.Net Core Web Application and add view

Open Visual Studio 2017. Go to File menu, point to new and click New project or click create new project link button on directly start page.

Add a view to an ASP.NET Core MVC app

The "New Project" window will open. You can select an installed template like “.NET Core” in Visual C# Template and then select the Asp.Net Core Web Application (.NET Core). Type project name, such as - MvcCore. Choose the project location path and click OK button. 

Add a view to an ASP.NET Core MVC app

The New ASP.NET Core Web Application (.NET Core) - MvcCore window will open. Select ASP.NET Core version 2.0 in the dropdown box. Select a template as Web Application (Model-View-Controller) and Then, click the OK button.

Add a view to an ASP.NET Core MVC app

Now, you can see MvcCore project structure, as shown in the screenshot given below.

Add a view to an ASP.NET Core MVC app

In Solution Explorer, go to the Controllers folder, right click it and point to Add, followed by clicking the New Item. 

Add a view to an ASP.NET Core MVC app

Now open a popup window, select controller template and click add button.

 Add a view to an ASP.NET Core MVC app

Now open (Add MVC Controller) popup window, enter controller Name and click add button.

 Add a view to an ASP.NET Core MVC app

Copy and paste the below code in UserController.cs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
 
namespace MvcCore.Controllers
{
    public class UserController: Controller
    {
        public IActionResult Index()
        {
            ViewBag.Title = "Index";
 
            ViewBag.Message= "User Page";
            return View();
        }
    }
}

In Solution Explorer, go to the Views folder, right-click it and point to Add, followed by clicking the New Folder. Then, name the folder as User.

Add a view to an ASP.NET Core MVC app

Again, in Views folder, go to the User folder, right-click it and point to Add, and click to view.

Add a view to an ASP.NET Core MVC app

Now open (Add MVC View ) popup window, Enter view name as Index, and click Add button.

Add a view to an ASP.NET Core MVC app

Copy and paste the below code into Views/User/Index.cshtml.

@{
    ViewData["Title"]= "User";
}
 
<h2>@ViewBag.Title</h2>
 
<p>@ViewBag.Message</p>

Pressing the F5 or CTRL + F5 button will run the app in debug mode, as shown in the below screenshot.

Add a view to an ASP.NET Core MVC app

I hope you understand now how to create a controller in asp.net mvc App using .Net Core 2.0 and Visual Studio 2017.

Please share your valuable feedback or comments to improve my future articles.

 

 

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like