English 中文(简体)
ASP.NET MVC - Getting Started
  • 时间:2024-11-05

ASP.NET MVC - Getting Started


Previous Page Next Page  

In this chapter, we will look at a simple working example of ASP.NET MVC. We will be building a simple web app here. To create an ASP.NET MVC apppcation, we will use Visual Studio 2015, which contains all of the features you need to create, test, and deploy an MVC Framework apppcation.

Create ASP.Net MVC Apppcation

Following are the steps to create a project using project templates available in Visual Studio.

Step 1 − Open the Visual Studio. Cpck File → New → Project menu option.

A new Project dialog opens.

Project Menu Option

Step 2 − From the left pane, select Templates → Visual C# → Web.

Step 3 − In the middle pane, select ASP.NET Web Apppcation.

Step 4 − Enter the project name, MVCFirstApp, in the Name field and cpck ok to continue. You will see the following dialog which asks you to set the initial content for the ASP.NET project.

Enter Project Name

Step 5 − To keep things simple, select the ‘Empty’ option and check the MVC checkbox in the Add folders and core references section. Cpck Ok.

It will create a basic MVC project with minimal predefined content.

Once the project is created by Visual Studio, you will see a number of files and folders displayed in the Solution Explorer window.

Created ASP.Net MVC Project

As you know that we have created ASP.Net MVC project from an empty project template, so for the moment the apppcation does not contain anything to run.

Step 6 − Run this apppcation from Debug → Start Debugging menu option and you will see a 404 Not Found Error.

404 Not Found Error

The default browser is, Internet Explorer, but you can select any browser that you have installed from the toolbar.

Add Controller

To remove the 404 Not Found error, we need to add a controller, which handles all the incoming requests.

Step 1 − To add a controller, right-cpck on the controller folder in the solution explorer and select Add → Controller.

Add Controller

It will display the Add Scaffold dialog.

Controller Scaffold Dialog

Step 2 − Select the MVC 5 Controller – Empty option and cpck ‘Add’ button.

The Add Controller dialog will appear.

Add Controller Dialog

Step 3 − Set the name to HomeController and cpck the Add button.

You will see a new C# file HomeController.cs in the Controllers folder, which is open for editing in Visual Studio as well.

C# HomeController

Step 4 − To make this a working example, let’s modify the controller class by changing the action method called Index using the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFirstApp.Controllers {
   pubpc class HomeController : Controller {
      // GET: Home
      pubpc string Index(){
         return "Hello World, this is ASP.Net MVC Tutorials";
      }
   }
}

Step 5 − Run this apppcation and you will see that the browser is displaying the result of the Index action method.

Index Action Method Advertisements