§ 2. 1 preparing 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 alsodownload and install ASP. net MVC 2 from www.asp.net/mvc /.
- If you don't have any of these, then the easiest way to get started is to download and use Microsoft '. web platform installer, which is available free of charge from www.microsoft.com/web /.
§ 2. 2 creating a new ASP. net mvc Project
You'll have a choice of two templates to start your new project:
- The ASP. net mvc 2 Web Application Template creates a small example application that you can build on. This includes des 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.
Let's get started: Open Visual Studio and go to file --> New-> Project
§ 2. 2.1 Adding the first Controller
Next, when homecontroller. CS appears, remove any code that it already contains, and replace
Whole homecontroller class with this:
Public class homecontroller: controller {Public String index () {return "Hello MVC ";}}
§ 2. 2.2 How does it know to invoke homecontroller?
Under the default routing configuration, you cocould request any of the following URLs and it wocould be handled byIndexAction onHomecontroller:
So, when a browser requests http: // yoursite/or http: // yoursite/home, it gets back the output fromHomecontroller'sIndex method. Right now, the output is the stringHello, world!.
§ 2. 3 rendering Web pages
The next step is to produce some HTML output.
§ 2. 3.1 Creating and rendering a view
Your existing controller, homecontroller, currently sends a plain-text string to the browser. In real applications you're more likely to generate an HTML document, and you do so by usingView. Rewrite the method as follows:
Public class homecontroller: controller {public actionresult index () {return view ();}}
If you run now you will see a error
Figure 2-5. Error message shown when ASP. net mvc can't find a view Template
Again, I'm showing you this error message so you can see exactly what the framework is trying to do and so you don't think there's some den magic happening.It tells you not just that it couldn't find any suitable view to render. but also where it tried looking for one.When the framework wants to find the default view for an action called index on a controller called homecontroller, it will check the four locations listed in Figure 2-5.
Figure 2-6.Adding a view template for the index action
Uncheck "select master page" (since we're not using master pages in this example) and then clickadd. this will create a brand new view file for you at the correct default location for your action method:
~ /Views/home/index. aspx.
We write the view code like follows:
<Body> <div> Hello World (from view )! </Div> </body>
Then press F5 to launch the application again, And You shoshould see your view template at work
Figure 2-7.Output from the View
Besides viewresult, there are other types of objects you can return from an action.For example,RedirectresultPerforms a redirection, andHttpunauthorizedresultForces the visitor to log in. These things are calledAction Results.
§ 2. 3.2 Adding Dynamic Output
Of course, the whole point of a Web application platform is the ability to construct and displayDynamic Output. In ASP. net mvc, it's the Controller's job to construct some data, and the view's job to render it as HTML.
Now, alter your homecontroller's index () Action method (again) to add a string into viewdata:
Public class homecontroller: controller {public actionresult index () {int hour = 2; viewdata ["greeting"] = (hour <12? "Goodmorning": "goodnoon"); Return view ();}}
And update your index. aspx view template as follows
<Body> <div> <% = viewdata ["greeting"] %> world (from view )! </Div> </body>
When you run the application you will see the follows: