Ten years ago, ASP.NET provided software developers with a means to write complex web applications with their existing Windows Forms skillsets. Today, it continues to be a robust framework for writing scalable applications, from small e-Commerce to enterprise-level CRM. But ten years later, with many business critical applications existing solely on the Web, the Forms paradigm is less relevant.
ASP.NET MVC, based on Ruby On Rails, is now in its second release, with a third around the corner—a powerful framework based on new and old ideas, ASP.NET MVC allows the developer to work with web protocols and standards, while still making use of the industry-standard development environment—Visual Studio.
By example, this book will take you through the key areas of ASP.NET MVC. We'll look at the MVC structure, data access, and ASP.NET MVC's compatibility with client scripting and dependency injection. With a minimal knowledge of web development, this book will help you build scalable web applications with relative ease.
Chapter 1, Working with the View... The view is possibly the most recognizable component of ASP.NET to a web forms developer, with its familiar ASPX extension. However, the differences are significant; we will explore the concepts of ViewData
, strongly typed Views, and helpers in this chapter.
Chapter 2, Taking Action in Your Controllers... For every view there is an action and a controller to host it, for it is the action that you navigate to in your browser. The view is merely a template to be called upon. In this chapter, we will learn about controllers and their actions, particularly the resulting types that are not necessarily HTML.
Chapter 3, Routing... If the action is a destination, routing is how you get there. The structure of a URL is an important aspect of a site's navigation; it should read like a map, showing you where you are and give hints as to where you can go. We'll look at how you can manage URLs to create user-friendly site maps, control errors, and provide a hackable API into the current view.
Chapter 4, Master Pages... Don't Repeat Yourself; every good developer should keep things DRY. Master Pages are one of many conventions for doing just that. Here, we'll look at creating a site-wide look and feel, nesting, and shared functions.
Chapter 5, Working with Data in the View... The difference between static HTML and dynamically generated sites is usually the data. In this chapter, we'll look at how easy it is to manage data in this powerful new framework and how it reintroduces old concepts to powerful effect.
Chapter 6, Working with Forms... Forms are the heart of the interactive Web, working at the browser's most basic level; they can be enhanced to provide a rich and intuitive experience. ASP.NET MVC provides extraordinary control over these elements; we'll see how we can capitalize on this control with client scripting and templates.
Chapter 7, Simplifying Complex Applications... ASP.NET MVC has been designed from the ground up to be tested, extended, and improved. Here we'll see how third-party libraries can be used to extend existing functionality or provide a completely new set of options.
Chapter 8, Validating MVC... Validation is the key to any application that accepts input of any kind, but where should it be implemented—on the client where it is most responsive, in the business logic where it is most central, or in the data layer where it is most secure? ASP.NET MVC provides a simple yet powerful framework for defining validation from a central location that is decoupled from the data layer; validation that makes use of client scripting without being dependent on it; and with all this, keeping the validation DRY. In this chapter, we'll look at data annotations, client-side scripting, and remote validation.
Chapter 9, Data Access and Storage... With so many options for storage these days, you might find that you don't want to tie yourself to just one. In this chapter, we'll look at the repository pattern and how, when used in conjunction with dependency injection, we can create an application that, with minimal work, can be connected to XML, SQL, or test data sources.
Chapter 10, Application, Session, Cookies, and Caching... Application optimization has never been easier with ASP.NET MVC. In this chapter, we'll look at session management using session and cookies, and how to cache different aspects of the application to provide the best performance.
This book assumes that you're using ASP.NET MVC 2, but most recipes will work in version 1 and the latest release candidates of version 3 with only minor modification. To run ASP.NET MVC 2, you will need at least Microsoft Visual Studio 2008 or Visual Web Developer 2008, for which ASP.NET MVC 2 is available as a separate download. If you're already using VS 2010 or VWD 2010, ASP.NET MVC 2 is already installed. If you're not using any version at the moment, I would recommend downloading and installing Visual Web Developer 2010, it's free and pretty great. A few of the chapters have dependencies on free third-party libraries, which are described in the chapters. However, Chapter 9 also assumes the installation of SQL Server Express (2008 or above); this was likely part of your original Visual Studio installation, but if not, it is also a free download.
This book is particularly written for web developers looking to transfer their knowledge from the ASP.NET web forms way of doing things to the ASP.NET MVC framework. As this book targets readers of various experience levels, you should be able to find recipes of a basic, intermediate, and advanced nature. Regardless of your experience level, each recipe will walk you through the solution in a step-by-step manner that anyone should be able to follow.
In this book, you will find a number of styles of text that distinguish among different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "Next, we'll add a function to the CModel
class that will allow us to set a given effect to any given mesh part."
A block of code is set as follows:
public ActionResult ReportComplete(string reportName, int fromYear, int fromMonth, int fromDay, int toYear, int toMonth, int toDay) { return View("Report"); }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Report</h2>
<%: ViewData["report"] %>
</asp:Content>
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: " In this recipe, we will take a look at a way to easily handle that pesky Page cannot be found error."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>
.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Note
Downloading the example code for this book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.