Introduction

IT's A GREAT TIME to be an ASP.NET developer!

Whether you've been developing with ASP.NET for years, or are just getting started, now is a great time to dig into ASP.NET MVC 3. ASP.NET MVC has been a lot of fun to work with from the start, but with features like the new Razor view engine, integration with the NuGet package management system, deep integration with jQuery, and powerful extensibility options, ASP.NET MVC 3 is just a lot of fun to work with!

With this new release, things have changed enough that we've essentially rewritten the book, as compared to the previous two releases. ASP.NET MVC team member Brad Wilson and noted ASP.NET expert K. Scott Allen joined the author team, and we've had a blast creating a fresh new book. Join us for a fun, informative tour of ASP.NET MVC 3!

Who This Book Is For

This book is for web developers who are looking to add more complete testing to their web sites, and who are perhaps ready for “something different.”

In some places, we assume that you're somewhat familiar with ASP.NET WebForms, at least peripherally. There are a lot of ASP.NET WebForms developers out there who are interested in ASP.NET MVC so there are a number of places in this book where we contrast the two technologies. Even if you're not already an ASP.NET developer, you might still find these sections interesting for context, as well as for your own edification as ASP.NET MVC 3 may not be the web technology that you're looking for.

It's worth noting, yet again, that ASP.NET MVC 3 is not a replacement for ASP.NET Web Forms. Many web developers have been giving a lot of attention to other web frameworks out there (Ruby on Rails, Django) which have embraced the MVC (Model-View-Controller) application pattern, and if you're one of those developers, or even if you're just curious, this book is for you.

MVC allows for (buzzword alert!) a “greater separation of concerns” between components in your application. We'll go into the ramifications of this later on, but if it had to be said in a quick sentence: ASP.NET MVC 3 is ASP.NET Unplugged. ASP.NET MVC 3 is a tinkerer's framework that gives you very fine-grained control over your HTML and Javascript, as well as complete control over the programmatic flow of your application.

There are no declarative server controls in MVC, which some people may like and others may dislike. In the future, the MVC team may add declarative view controls to the mix, but these will be far different from the components that ASP.NET Web Forms developers are used to, in which a control encapsulates both the logic to render the view and the logic for responding to user input, etc. Having all that encapsulated in a single control in the view would violate the “separation of concerns” so central to this framework. The levels of abstraction have been collapsed, with all the doors and windows opened to let the air flow freely.

The final analogy we can throw at you is that ASP.NET MVC 3 is more of a motorcycle, whereas ASP.NET Web Forms might be more like a minivan, complete with airbags and a DVD player in case you have kids and you don't want them to fight while you're driving to the in-laws for Friday dinner. Some people like motorcycles, some people like minivans. They'll both get you where you need to go, but one isn't technically better than the other.

How This Book Is Structured

This book is divided into two very broad sections, each comprising several chapters.

The first half of the book is concerned with introducing the MVC pattern and how ASP.NET MVC implements that pattern.

Chapter 1 helps you get started with ASP.NET MVC 3 development. It explains what ASP.NET MVC is and explains how ASP.NET MVC 3 fits in with the previous two releases. Then, after making sure you have the correct software installed, you'll begin creating a new ASP.NET MVC 3 application.

Chapter 2 then explains the basics of controllers and actions. You'll start with some very basic “hello world” examples, then build up to pull information from the URL and return it to the screen.

Chapter 3 explains how to use view templates to control the visual representation of the output from your controller actions. You'll learn all about Razor, the new view engine that's included in ASP.NET MVC 3.

Chapter 4 teaches you the third element of the MVC pattern: the model. In this Chapter, you'll learn how to use models to pass information from controller to view and how to integrate your model with a database (using Entity Framework 4.1).

Chapter 5 dives deeper into editing scenarios, explaining how forms are handled in ASP.NET MVC. You'll learn how to use HTML Helpers to keep your views lean.

Chapters 6 explains how to use attributes to define rules for how your models will be displayed, edited, and validated.

Chapter 7 teaches you how to secure your ASP.NET MVC application, pointing out common security pitfalls and how you can avoid them. You'll learn how to leverage the ASP.NET membership and authorization features within ASP.NET MVC applications to control access.

Chapter 8 covers Ajax applications within ASP.NET MVC applications, with special emphasis to jQuery and jQuery plugins. You'll learn how to use ASP.NET MVC's Ajax helpers, and how to work effectively with the jQuery powered validation system that's included in ASP.NET MVC 3.

Chapter 9 digs deep into the routing system that manages how URL's are mapped to controller actions.

Chapter 10 introduces you to the NuGet package management system. You'll learn how it relates to ASP.NET MVC, how to install it, and how to use it to install, update, and create new packages.

Chapter 11 explains dependency injection, the changes ASP.NET MVC 3 includes to support it, and how you can leverage it in your applications.

Chapter 12 teaches you how to practice test driven development in your ASP.NET applications, offering helpful tips on how to write effective tests.

Chapter 13 dives into the extensibility points in ASP.NET MVC, showing how you can extend the framework to fit your specific needs.

Chapter 14 looks at advanced topics that might have blown your mind before reading the first 13 chapters of the book. It covers sophisticated scenarios in Razor, scaffolding, routing, templating, and controllers.

What You Need to Use This Book

To use ASP.NET MVC 3, you'll probably want a copy of Visual Studio. You can use Microsoft Visual Web Developer 2010 Express, or any of the paid versions of Visual Studio 2010 (such as Visual Studio 2010 Professional). Visual Studio 2010 includes ASP.NET MVC 3.

The following list shows you where to go to download the required software:

Chapter 1 reviews the software requirements in depth, showing how to get everything set up on both your development and server machines.

Conventions

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.

Occasionally the product team will take a moment to provide an interesting aside or four-bit of trivia, and those will appear in boxes like the one below.

Product Team Aside

UnFigure

Boxes like this one hold tips, tricks, trivia from the ASP.NET Product Team or some other information that is directly relevant to the surrounding text.

 

note

Tips, hints and tricks to the current discussion are offset and placed in italics like this.

As for styles in the text:

  • We italicize new terms and important words when we introduce them.
  • We show keyboard strokes like this: Ctrl+A.
  • We show file names, URLs, and code within the text like so: persistence.properties.
  • We present code in two different ways:
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present
context or to show changes from a previous code snippet.

Source Code

You'll notice that throughout the book, we have places where we suggest that you install a NuGet package to try out some sample code.

UnFigure
Install-Package SomePackageName

NuGet is a new package manager for .NET and Visual Studio written by the Outercurve Foundation and incorporated by Microsoft into ASP.NET MVC.

Rather than having to search around for zip files on the Wrox website for source code samples, you can use NuGet to easily add these files into an ASP.NET MVC application from the convenience of Visual Studio. We think this will make it much easier and painless to try out the samples and hopefully you're more likely to do so.

Chapter 10 explains the NuGet system in greater detail.

In some instances, the book covers individual code snippets which you may wish to download. This code is available for download at www.wrox.com. Once at the site, simply locate the book's title (use the Search box or one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book. Code that is included on the Web site is highlighted by the following icon:

download

Listings include the filename in the title. If it is just a code snippet, you'll find the filename in a code note such as this:

Code snippet filename

note

Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-1-118-07658-3.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book's errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot “your” error on the Book Errata page, go to and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to p2p.wrox.com and click the Register link.

2. Read the terms of use and click Agree.

3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

note

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

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

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