15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

M VC ❘ 1261<br />

a simple asP.neT mVC application<br />

In this section, you will build an ASP.<strong>NET</strong> MVC application using the MagicShop.mdf database introduced<br />

earlier in this chapter. Along the way, you will walk through the code responsible for models, views, <strong>and</strong><br />

controllers <strong>and</strong> see how it all fi ts together, before adding some additional customizations.<br />

Creating the application<br />

To get started, create a new project in Visual Studio via the File ➪ New ➪ Project . . . menu item. Select the<br />

ASP.<strong>NET</strong> MVC 2 Web Application template, call the project PCSMVCMagicShop, <strong>and</strong> save it in the C:\<br />

ProCSharp\Chapter42 directory. You will be prompted as to whether to create a unit test application at the<br />

same time, but for now deselect that option (unit testing is beyond the scope of this chapter).<br />

Note that there is no template for ASP.<strong>NET</strong> MVC if you use the File ➪ New ➪ Web<br />

Site … menu item. ASP.<strong>NET</strong> MVC applications must be web applications , not web<br />

sites, as they require sites to be fully compiled before deployment in order to work.<br />

This is because of the interconnectedness of the modules that make up an ASP.<strong>NET</strong><br />

MVC application <strong>and</strong> the way in which views are dynamically generated.<br />

inspecting the application<br />

When the wizard has completed, you will notice that a fair number of directories <strong>and</strong> fi les have been created<br />

for you. These are described in the following table.<br />

iTem<br />

desCriPTion<br />

Content directory This directory, like the Content directory for a dynamic data web site, is intended<br />

for images <strong>and</strong> other miscellaneous resources that are used by the application.<br />

Initially, it contains the CSS fi le used by the web application, Site.css .<br />

Controllers directory This directory contains the controller classes used by the site. The template includes<br />

two controllers to get you started, HomeController <strong>and</strong> AccountController .<br />

Models directory This directory, initially empty, is where you should put your models. For example,<br />

you could add LINQ to SQL classes, ADO.<strong>NET</strong> Entity Framework classes, or<br />

whatever custom classes you see fi t here.<br />

Scripts directory This directory contains any script fi les, such as JavaScript fi les, used by the application.<br />

You will fi nd a fair amount of JavaScript fi les here by default, including<br />

those required for the Microsoft jquery <strong>and</strong> AJAX libraries.<br />

Views directory This directory contains the views for the application, which are ASP.<strong>NET</strong> Web Forms<br />

<strong>and</strong> user controls. Each controller has its own views subdirectory, so you will see<br />

content in Home <strong>and</strong> Account directories by default. You will also see a Shared<br />

directory that includes general purpose views <strong>and</strong> master pages, including Site<br />

.Master , which is the default master page for the site. There is also a Web.config<br />

fi l e i n t h i s d i re c to r y, w h ich i s co n fi g u re d to p reve n t d i re c t a cces s to t h e v iew fi l es .<br />

Global.asax The Global.asax file (<strong>and</strong> its code - behind file) included in the application root<br />

sets up routing as described earlier in this chapter. ASP.<strong>NET</strong> MVC defines extension<br />

methods on the RouteCollection class to make it easier to add routes in the way<br />

it requires.<br />

Web.confi g This confi guration fi le references all of the additional resources required by<br />

ASP.<strong>NET</strong> MVC, including the required assemblies. It also confi gures the HTTP<br />

h<strong>and</strong>ler that is used to dynamically generate views according to the routing<br />

path requested, <strong>and</strong> (due to the project template) confi gures forms - based<br />

authentication.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!