256Microsoft Visual Studio 2010: A Beginner’s Guide

TIP

You can change your VS Web server’s port number. If you open your project’s property page by right-mouse clicking on the project in Solution Explorer and select Properties, then select the Web tab on the left, under Servers, you can specify a specific port or make other Web server choices.

For ASP.NET MVC, the important part of the URL is /Home/About. Home is the name of the Controller, and ASP.NET MVC appends Controller to the URL name, looking for the HomeController class, shown in Listing 9-1, physically located in the Controller folder, which is why it’s important to ensure you create files in the proper locations. About is an action, which corresponds to the About method shown in Listing 9-1. Similar to the About method, the Index action is run through the following URL:

http://localhost:1042/Home/Index

In a later section of this chapter, you’ll learn how ASP.NET MVC performs routing, which maps URLs to Controllers.

Both the Index and About actions in Listing 9-1 invoke a method named View. This is a convention for invoking a View with the same name as the action method. For example, calling View in the Index action will show a View named Index, and the call to View in the About method will show a View named About.

One more item to point out is how the Index action assigns a string to a collection

called ViewData. The ViewData collection is one way for a Controller to pass Model data to a View. I’ll cover more on Controllers, including how to create your own, in a later part of this chapter, but now, let’s do a quick review of Views so that you can see what happens when they are invoked by the Controller.

Displaying Views

A View is what displays in the browser and allows interaction with the user. The View can display any information that a Controller passes to it. For example, notice that the Index action in Listing 9-1 assigns a string “Welcome to ASP.NET MVC!” with the “Message” key in the ViewData collection.

Looking Inside a View

Figure 9-3 shows the View in the browser, displaying the message. Listing 9-2 shows the Hypertext Markup Language (HTML) of the View displaying the message. The View actually has a combination of HTML and ASP.NET markup, sometimes referred to as ASPX, but I’ll refer to it as just HTML for the rest of the chapter.

Page 279
Image 279
Microsoft 9GD00001 manual Displaying Views, Looking Inside a View