In this post we shall create a Hello
World Asp.Net MVC 2 Application from the scratch. We will make use of the Empty
Web Application template to create this sample so that we will understand the
basics of MVC. Follow the below steps to create the first MVC 2 Hello World Application
2. Create a new Project of type Asp.Net MVC 2 Empty Web Application
3. Give a Name, Location and save the Project
4. This will create a basic Project structure.
5. Right click on the View folder and select Add -> New Folder
6. Name the folder as “HelloMVC”
7. Now Right click the HelloMVC folder and select Add -> View
8. Name the file as “Index”
9. If the “Select Master Page” checkbox is checked, remove it and click Add
10. Now, Right click on the Controllers folder and select Add -> Controller
11. Name the controller as “HelloMVCController” and click Add
12. Open the controller file and add a Message key to the ViewData collection as follows.
using System;
using
System.Collections.Generic;
using
System.Linq;
using System.Web;
using
System.Web.Mvc;
namespace
MvcSample.Controllers
{
public class HelloMVCController
: Controller
{
//
// GET:
/HelloMVC/
public ActionResult Index()
{
ViewData["Message"]
= "Hello ASP.NET MVC";
return
View();
}
}
}
11. Now open the Index.aspx View file,
and modify the code to read the Message key as follows.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head runat="server">
<title>HelloMVC</title>
</head>
<body>
<div>
<h2><%= Html.Encode(ViewData["Message"])
%></h2>
</div>
</body>
</html>
12. Now, Build and run the application
13. In the address bar append /HelloMVC/ to the root path.
http://localhost:3655/HelloMVC/
14. The page opens up and displays “Hello ASP.NET MVC”
15. That’s it we have created a Hello World MVC Application.
No comments:
Post a Comment