- ASP.NET MVC - Self-hosting
- ASP.NET MVC - Deployment
- ASP.NET MVC - Unit Testing
- ASP.NET MVC - Bootstrap
- ASP.NET MVC - Scaffolding
- ASP.NET MVC - Web API
- Nuget Package Management
- ASP.NET MVC - Data Annotations
- ASP.NET MVC - Razor
- ASP.NET MVC - Caching
- ASP.NET MVC - Security
- ASP.NET MVC - Validation
- ASP.NET MVC - Databases
- ASP.NET MVC - Model Binding
- ASP.NET MVC - Helpers
- ASP.NET MVC - Data Model
- ASP.NET MVC - Views
- ASP.NET MVC - Selectors
- ASP.NET MVC - Filters
- ASP.NET MVC - Actions
- ASP.NET MVC - Controllers
- ASP.NET MVC - Routing
- ASP.NET MVC - Life Cycle
- ASP.NET MVC - Getting Started
- ASP.NET MVC - Environment Setup
- ASP.NET MVC - Pattern
- ASP.NET MVC - Overview
- ASP.NET MVC - Home
ASP.NET MVC Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ASP.NET MVC - Getting Started
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.
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.
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.
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.
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.
It will display the Add Scaffold dialog.
Step 2 − Select the MVC 5 Controller – Empty option and cpck ‘Add’ button.
The Add Controller dialog will appear.
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.
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.
Advertisements