Wednesday, 30 December 2009

Hello World example in MVC ASP.Net.

Create a project of MVC from your Visual Studio 2008:




After selecting ASP.Net MVC Web Application change the name to “HelloWorld” and press OK.

Don’t select yes for Test Project as we don’t need testing for this simple example here is the screen shot of project created by wizard for us which have three main folders:

1- Controllers – All the logic to control the flow of pages.

2- Model: All business logics.

3- Views: All front end designs

If you run this project you can see the default page like this:

Our goal for this blog is to change the highlighted text which is “Welcome to Asp.Net MVC!” to “Welcome to Hello World!”

Now look into the folders you will find HomeController class in Controller folder where you can see a Action Index() this is responsible to set the text for the ViewData[“Message”] and return it to View.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace HelloWorld.Controllers

{

[HandleError]

public class HomeController : Controller

{

public ActionResult Index()

{

ViewData["Message"] = "Welcome to Hello World!";

return View();

}

public ActionResult About()

{

return View();

}

}

}



Now the question is how this message is set at View. Have a look in Views Folder and then Home Folder there is Index.aspx file which is a view for Home and About.aspx file is for About View which works when you click on the respective tabs:

Here is the code for Index.aspx:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">

Home Page

asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

<h2><%= Html.Encode(ViewData["Message"]) %>h2>

<p>

To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvca>.

p>

asp:Content>

Here the text sets up <h2><%= Html.Encode(ViewData["Message"]) %>h2>

Now go back to HomeController and change the text in Index Action to:

From:

public ActionResult Index()

{

ViewData["Message"] = "Welcome to ASP.NET MVC!";

return View();

}

To:

public ActionResult Index()

{

ViewData["Message"] = "Welcome to Hello World!";

return View();

}

And run the project now:

Download the code here.

3 comments:

Raza Khan said...

Really a basic one for those who want to start with MVC please also upload some of your vidoes if you have.

Anonymous said...

Νοne has гesponded yеt. I hope yοu businеѕѕ
cаn join for free anԁ then find out just
what you ѕhоulԁ knoω the basіcs.
We use busіnеss thе vinyl lettering. Machines with еxtremely low flow ratеs of 0.
Unfavorable сuггencу exchаnge rates is known aѕ thе Gіni сoefficient was at 0.
In сonsulting, our words arе our most powеrful tool.


Mу homеpаge: affiliate internet marketing business

Anonymous said...

Successful search engine marketing experts have been able to make a profit or to avoid
the risk of having your website deemed to be spam.
Uncreative choice, I know. Not at all; links are still good for search engine marketing.


Feel free to visit my webpage: http://articleseachengine.com/article.php?id=896