INTRODUCTION

WHY DOES THE WORLD NEED yet another web framework?

This is the question that is most likely on your mind-or perhaps it's what you were thinking when you saw this book sitting on the shelf. We each asked ourselves this many times over the last few years.

Indeed there are many frameworks out there today flavored with every buzzword the industry can think of. In short: it's easy to be skeptical. Yet as we, the authors, delve deeper into the latest and greatest web framework, we're each starting to realize just how far the industry has come in the last 10 years.

Rob began programming for the web with Classic ASP in 1997 and was giddy with excitement. When .NET came out he remembers running around his office, stopping everyone from working and explaining that the world just tilted on its axis.

We all feel the same way about ASP.NET MVC 2. Not because it's "something different," but because it offers developers the ultimate chance to "do it their way." You don't like the way the platform renders the View? Change it! Just about every part of the ASP.NET MVC 2 Framework is "swappable" — if the shoes pinch, get different shoes. Don't like ties? Why not a bow-tie? You're totally in control.

ASP.NET MVC 2 is a web framework that comes with a bunch of conventions to make your life easier when you follow them, but if you don't want them, the Framework is quick to step out of your way so you can get your work done in the way you like it done.

This book is going to go into the "out of the box" experience you'll have with ASP.NET MVC 2, but more importantly we'll learn practical ways that you can extend ASP.NET MVC 2 with your own magic — then hopefully share that magic with others.

Because of this extensibility and attention to "doing it your way," we're happy to embrace Yet Another Web Framework and hope you are willing to come along with us for the ride.

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 2 may not be the web technology that you're looking for.

It's worth noting, yet again, that ASP.NET MVC 2 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 it in a quick sentence: ASP.NET MVC 2 is ASP.NET Unplugged. ASP.NET MVC 2 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, 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 2 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 mini vans. 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 starts off with a description of the Model-View-Controller pattern, explaining the basic concepts of the pattern and providing a bit of its history. The chapter goes on to describe the state of the MVC pattern on the web today as it is implemented by various frameworks, such as ASP.NET MVC 2.

Chapter 2 then covers the ways that ASP.NET MVC is different from ASP.NET Web Forms and how to get ASP.NET MVC 2 up and running.

Chapter 3 explores the structure of a standard MVC application and covers what you get out of the box. It covers some of the conventions and the digs a little under the hood to take a look at the entire request lifecycle for an ASP.NET MVC 2 request.

Chapter 4 digs deep into routing to describe the role that URLs play in your application and how routing figures into that. It also differentiates routing from URL rewriting and covers a bit on extending routing and writing unit tests for routes.

Chapter 5 takes a look at controllers and controller actions — what they are, and how to write them. It also covers action results, which are returned by controller actions and what they are used for.

Chapters 6 and 7 cover views and view engines, and then add a little flavor on top by examining the role that AJAX plays in your views.

The second half of the book focuses entirely on advanced techniques and extending the framework.

Chapter 8 goes into detail on action filters, which provide an extensibility point for adding cross-cutting behaviors to action methods.

Chapter 9 covers security and good practices for building a secure application.

Chapter 10 provides a brief introduction to Test Driven Development (TDD) as it applies to ASP.NET MVC 2.

Chapter 11 shows you real-world patterns and practices for building applications that are testable.

Chapter 12 discusses how Web Forms and MVC work together.

Chapter 13 covers what's new in MVC 2.

WHAT YOU NEED TO USE THIS BOOK

To use ASP.NET MVC 2, 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 2.

You can also use Visual Studio 2008 Web Developer Express SP1, or any of the paid versions of Visual Studio 2008 (such as Visual Studio 2008 Professional). If you're going to use the Web Developer Express edition of Visual Studio, you need to confirm that you're using SP1. ASP.NET MVC 2 requires that you use Web Application Projects (WAPs) rather than Web Sites Projects; this functionality was added in SP1 of Web Developer Express. You will also need to make sure that you have the .NET Framework 3.5 installed at minimum. The runtime does not require .NET 3.5 SP1 to run.

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

  • Visual Studio or Visual Studio Express: www.microsoft.com/vstudio or www.microsoft.com/express/

  • ASP.NET MVC 2: www.asp.net/mvc

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 us an interesting aside, four-bit of trivia, and those will appear in boxes like this

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

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book 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:

SOURCE CODE

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-0-470-643181.

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 www.wrox.com/contact/techsupport.shtml 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