MVC 2 Asp.Net Tutorial : Create Hello World Web Application

Leave a Comment

Before you can write any ASP.NET MVC 2 code, you need to install the relevant development tools on your workstation. To build an ASP.NET MVC 2 application, you need either of the following

Visual Studio 2010 (any edition) or the free Visual Web Developer 2010 Express. These include ASP.NET MVC 2 by default.

Visual Studio 2008 with SP1 (any edition) or the free Visual Web Developer 2008 Express with SP1. These do not include ASP.NET MVC 2 by default; you must also download and install ASP.NET MVC 2.

Create New ASP.NET MVC Project

Let’s get started: open Visual Studio and go to File -> New -> Project, first open the Web category, then make sure the framework selector (top right) reads .NET Framework 4 or .NET Framework 3.5 (if you’re using Visual Studio 2008, you must choose .NET Framework 3.5), and then select ASP.NET 2 Empty MVC Web Application.

Note that : The ASP.NET MVC 2 Web Application template creates a small example application that you can build on. This includes prebuilt user registration, authentication, navigation, and a relaxing, blue-themed CSS stylesheet. The ASP.NET MVC 2 Empty Web Application template sets up only the minimal set of files and folders that are needed for almost every ASP.NET MVC 2 application.

Name the project anything you like, but a good name would be HelloWorld. When you click OK, Visual Studio will set up a default project structure for you.

MVC 2 Asp.Net


Add Controller

In model-view-controller (MVC) architecture, incoming requests are handled by controllers. In ASP.NET MVC, controllers are just simple C# classes (usually inheriting from System.Web.Mvc.Controller, the framework’s built-in controller base class). Each public method on a controller is known as an action method, which means you can invoke it from the Web via some URL.

So, to add the first controller, right-click the Controllers folder in Visual Studio’s Solution Explorer and choose Add -> Controller. When the Add Controller prompt appears, enter the name HomeController and then click Add.

MVC 2 Asp.Net

MVC 2 Asp.Net

Next, when HomeController.cs appears, remove any code that it already contains, and replace the whole HomeController class with this:

public class HomeController : Controller
    {
        public string Index()
        {
            return "Hello World";
        }
    }


MVC 2 Asp.Net

Try running the project now (press F5 again), and you should see below message displayed in a browser :

MVC 2 Asp.Net

0 comments:

Post a Comment