Tuesday 3 June 2014

Creating a New ASP.NET MVC Project

We are going to start by creating a new MVC project in Visual Studio. Select New Project from the File menu to open the New Project dialog. If you select the Web templates, you’ll see that the MVC 3 installer has created a new item called ASP.NET MVC 3 Web Application, as shown in Figure 1


Set the name of the new project to PartyInvites and click the OK button to continue. You will see another dialog box, shown in Figure 2, which asks you to choose between three different types of MVC project templates.


The Empty option creates a project with only the minimum files and folders required for an MVC 3 application. The Internet Application option creates a small example application that you can modify and build on. It includes user registration and authentication, navigation, and a consistent visual style.
The Intranet Application option is similar to Internet Application, but is designed for use in environments that authenticate users through a domain/Active Directory infrastructure. For this chapter, we are going to keep things simple. Select the Empty option, leave the Use HTML5 semantic
markup option unchecked, and click OK to create the new project.
Once Visual Studio creates the project, you’ll see a number of files and folders displayed in the Solution Explorer window. This is the default structure for an MVC 3 project. You can try to run the application now by selecting Start Debugging from the Debug menu (if it prompts you to enable debugging, just click the OK button). You can see the result in Figure 3-3. Since we started with the empty project template, the application doesn’t contain anything to run, so we see a 404 Not Found Error. 


When you’re finished, be sure to stop debugging by closing the browser window that shows the error, or by going back to Visual Studio and selecting Stop Debugging from the Debug menu.

Adding the First Controller

In MVC architecture, incoming requests are handled by controllers. In ASP.NET MVC, controllers are just simple C# classes (usually inheriting from system.Web.Mvc.Controller, the framework’s built-in controller base class). Each public method in a controller is known as an action method, meaning you
can invoke it from the Web via some URL to perform an action. The MVC convention is to put controllers in a folder called Controllers, which Visual Studio created for us when it set up the project.
You don’t need to follow this or most other MVC conventions, but we recommend that you do—not least because it will help you make sense of the examples in this book.
To add a controller to our project, right-click the Controllers folder in the Visual Studio Solution Explorer window and choose Add and then Controller from the pop-up menus, as shown in Figure -4.


When the Add Controller dialog appears, set the name to HomeController, as shown in Figure 3-5.
This is another convention: the names we give to controllers should be descriptive and end with Controller.


The Scaffolding options section of the dialog allows us to create a controller using a template with common functions. We aren’t going to use this feature, so ensure that the Empty controller item is selected in the Template menu, as shown in the figure.
Click the Add button to create the controller. Visual Studio will create a new C# code file in the Controller folder called HomeController.cs and open it for editing. You can see that the class is called HomeController and it is derived from System.Web.Mvc.Controller. Edit the code in this file so that it matches Listing -1.
Listing -1. Modifying the HomeController Class
using System.Web.Mvc;
namespace PartyInvites.Controllers {
   public class HomeController : Controller {
      public string Index() {
         return "Hello, world";
      }
   }
}

We haven’t created anything exciting, but this is a good way of getting started with MVC. We’ve created an action method called Index, which returns the string “Hello, world”. Run the project again by selecting Start Debugging from the Visual Studio Debug menu. The browser will display the result of the
Index action method, as shown in Figure -6.




Who Should Use ASP.NET MVC?


  • As with any new technology, the fact of ASP.NET MVC’s existence isn’t a compelling reason to adopt it.
  • Here, we’ll give you our view of how the MVC Framework compares with the most obvious alternatives.
  • We’ve tried to be as unbiased as two people writing a book about the MVC Framework can be, but we know that there is a limit to our objectivity. The following sections are technology-based comparisons.
  • When selecting a web application framework, you should also consider the skills of your team, the work involved in porting any existing projects, and your relationship with, and confidence in, the technology source.


Comparisons with ASP.NET Web Forms
We have already detailed the weaknesses and limitations in traditional ASP.NET Web Forms, and how ASP.NET MVC overcomes many of those problems. That doesn’t mean that Web Forms is dead, though.
Microsoft has repeatedly stated that both technologies are being actively developed and actively supported, and that there are no plans to retire Web Forms. In some ways, your choice between the two is a matter of development philosophy. 

Consider these points:

  • Web Forms takes the view that UIs should be stateful, and to that end, adds a sophisticated abstraction layer on top of HTTP and HTML, using View State and postbacks to create the effect of statefulness. 
  • This makes it suitable for drag-anddrop Windows Forms–style development, in which you pull UI widgets onto a canvas and fill in code for their event handlers.
  • MVC embraces HTTP’s true stateless nature, working with it rather than fighting against it. 
  • The MVC Framework requires you to understand how web applications actually work. 
  • Given that understanding, it provides a simple, powerful,  modern approach to writing web applications, with tidy code that’s easier to extend and maintain over time, and that’s free of bizarre complications and painful limitations. 
  • There are certainly cases where Web Forms is at least as good as, and probably better than, MVC.
  • The obvious example is small, intranet-type applications that are largely about binding grids directly to database tables or stepping users through a wizard. 
  • Web Forms drag-and-drop development strengths can outweigh its weaknesses when you don’t need to worry about bandwidth consumption or search engine optimization.
  • If, on the other hand, you are writing applications for the Internet or larger intranet applications, you will be attracted by the bandwidth efficiency, better browser compatibility, and better support for automated testing that MVC offers.