13.3. An Existing MVC Application

I think it is useful to start by looking at what a popular application built using MVC looks like. You might be familiar with the Stack Overflow (SO) site.

SO is a popular programming web site in which users can ask programming questions to be answered by other users. Other users then answer the questions. These answers are then voted on by other users; those with the most votes (ideally the best ones!) move to the top of the answer list.

Let's take a look at SO now:

  1. Open a browser, and go to http://www.stackoverflow.com.

  2. Take a look around the site, paying particular attention to the URL of each page.

  3. Imagine that you were creating an application similar to SO but were writing it using web forms. How might you construct it?

At a very simple level, in a traditional ASP.NET application, you would probably have two pages: one to list all the questions users have asked and another page to display the actual question and answers.

From the question list page, you would then pass a question ID in the query string through to the detail page and retrieve the relevant answers. For example, when a user clicks a question, the URL might look something like www.stackoverflow.com/questionDetail.aspx?id=3434.

Now click any question in SO, and take a look at the URL. You will see something similar to http://stackoverflow.com/questions/294017/visual-studio-2005-freezes.

This URL is superior to the query string version for a number of reasons:

  • Search engines index it better than a query string version. At the time of writing, Google seems to give a higher precedence to pages with the search term in the URL.

  • It's more readable to humans. If you have a product site, it's a lot easier for users to remember an address like www.microsoft.com/vs2010/ than www.microsoft.com/product/productDetail.aspx?id=4563432234.

  • The URL can assist other developers integrating with your application to understand how it works. For example, if you examine the question detail page (as shown in Figure 13-1), you will see the answer posts to /questions/740316/answer. You can probably figure out that 740316 is an ID for the question, and it wouldn't be too tricky to develop an addition to post answers.

Figure 13.1. Stack Overflow web siteā€”an example of an ASP.NET MVC application

This facility is called routing, and although it isn't specific to ASP.NET MVC, it is an important concept.

Routing is available in ASP.NET 4.0 and .NET 3.5 SP1 (see Chapter 10 for more details), so don't think you have to use ASP.NET MVC to take advantage of this.


ASP.NET MVC uses the requested URL to decide how to route users' requests to a special class called a controller that in turn interacts with your application's model.

Before you get started with creating a simple application, you should also be aware of the following:

  • MVC has no viewstate.

  • How to use type initialization syntax (introduced in C# 2008).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.202