Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu đang bị lỗi
File tài liệu này hiện đang bị hỏng, chúng tôi đang cố gắng khắc phục.
Pro ASP.NET MVC 5
Nội dung xem thử
Mô tả chi tiết
Pro ASP.NET MVC 5
Adam Freeman
Pro ASP.NET MVC 5
Copyright © 2013 by Adam Freeman This work is subject to copyright. All rights are reserved by the
Publisher, whether the whole or part of the material is concerned, specifically the rights of translation,
reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other
physical way, and transmission or information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from
this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material
supplied specifically for the purpose of being entered and executed on a computer system, for
exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is
permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current
version, and permission for use must always be obtained from Springer. Permissions for use may be
obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution
under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-6529-0
ISBN-13 (electronic): 978-1-4302-6530-6
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images
only in an editorial fashion and to the benefit of the trademark owner, with no intention of
infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they
are not identified as such, is not to be taken as an expression of opinion as to whether or not they are
subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for
any errors or omissions that may be made. The publisher makes no warranty, express or implied, with
respect to the material contained herein.
President and Publisher: Paul Manning Lead Editor: Ewan Buckingham
Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Steve Anglin, Mark
Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Jim DeWolf, Jonathan
Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham,
Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke,
Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss Coordinating Editor:
Kevin Shea Copy Editor: Larissa Shmailo
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer
Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013.
Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springersbm.com, or visit www.springeronline.com. Apress Media, LLC is a California
LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference
our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this text is available to
readers at www.apress.com/9781430257882. For detailed information about how to locate
your book’s source code, go to www.apress.com/source-code/.
Dedicated to my lovely wife, Jacqui Griffyth.
Contents at a Glance
About the Author
About the Technical Reviewer
Chapter 1: Putting ASP.NET MVC in Context
Chapter 2: Your First MVC Application
Chapter 3: The MVC Pattern
Chapter 4: Essential Language Features
Chapter 5: Working with Razor
Chapter 6: Essential Tools for MVC
Chapter 7: SportsStore: A Real Application
Chapter 8: SportsStore: Navigation
Chapter 9: SportsStore: Completing the Cart
Chapter 10: SportsStore: Mobile
Chapter 11: SportsStore: Administration
Chapter 12: SportsStore: Security & Finishing Touches
Chapter 13: Deployment
Chapter 14: Overview of MVC Projects
Chapter 15: URL Routing
Chapter 16: Advanced Routing Features
Chapter 17: Controllers and Actions
Chapter 18: Filters
Chapter 19: Controller Extensibility
Chapter 20: Views
Chapter 21: Helper Methods
Chapter 22: Templated Helper Methods
Chapter 23: URL and Ajax Helper Methods
Chapter 24: Model Binding
Chapter 25: Model Validation
Chapter 26: Bundles
Chapter 27: Web API and Single-page Applications
Index
Contents
About the Author
About the Technical Reviewer
Chapter 1: Putting ASP.NET MVC in Context
Understanding the History of ASP.NET
What Is Wrong with ASP.NET Web Forms?
Web Development Today
Web Standards and REST
Agile and Test-Driven Development
Ruby on Rails
Node.js
Key Benefits of ASP.NET MVC
MVC Architecture
Extensibility
Tight Control over HTML and HTTP
Testability
Powerful Routing System
Built on the Best Parts of the ASP.NET Platform
Modern API
ASP.NET MVC Is Open Source
What Do I Need to Know?
What Is the Structure of This Book?
Part 1: Introducing ASP.NET MVC 5
Part 2: ASP.NET MVC in Detail
What’s New in this Edition?
Where Can I Get the Example Code?
What Software Do I Need for This Book?
Credits
Summary
Chapter 2: Your First MVC Application
Preparing Visual Studio
Creating a New ASP.NET MVC Project
Adding the First Controller
Understanding Routes
Rendering Web Pages
Creating and Rendering a View
Adding Dynamic Output
Creating a Simple Data-Entry Application
Setting the Scene
Designing a Data Model
Linking Action Methods
Building the Form
Setting the Start URL
Handling Forms
Adding Validation
Styling the Content
Completing the Example
Summary
Chapter 3: The MVC Pattern
The History of MVC
Understanding the MVC Pattern
Understanding the Domain Model
The ASP.NET Implementation of MVC
Comparing MVC to Other Patterns
Building Loosely Coupled Components
Using Dependency Injection
Using a Dependency Injection Container
Getting Started with Automated Testing
Understanding Unit Testing
Understanding Integration Testing
Summary
Chapter 4: Essential Language Features
Preparing the Example Project
Adding the System.Net.Http Assembly
Using Automatically Implemented Properties
Using Object and Collection Initializers
Using Extension Methods
Applying Extension Methods to an Interface
Creating Filtering Extension Methods
Using Lambda Expressions
Using Automatic Type Inference
Using Anonymous Types
Performing Language Integrated Queries
Understanding Deferred LINQ Queries
Using Async Methods
Applying the async and await Keywords
Summary
Chapter 5: Working with Razor
Preparing the Example Project
Defining the Model
Defining the Controller
Creating the View
Working with the Model Object
Working with Layouts
Creating the Layout
Applying a Layout
Using a View Start File
Demonstrating Shared Layouts
Using Razor Expressions
Inserting Data Values
Setting Attribute Values
Using Conditional Statements
Enumerating Arrays and Collections
Dealing with Namespaces
Summary
Chapter 6: Essential Tools for MVC
Preparing the Example Project
Creating the Model Classes
Adding the Controller
Adding the View
Using Ninject
Understanding the Problem
Adding Ninject to the Visual Studio Project
Getting Started with Ninject
Setting up MVC Dependency Injection
Creating Chains of Dependency
Specifying Property and Constructor Parameter Values
Using Conditional Binding
Setting the Object Scope
Unit Testing with Visual Studio
Creating the Unit Test Project
Creating the Unit Tests
Running the Unit Tests (and Failing)
Implementing the Feature
Testing and Fixing the Code
Using Moq
Understanding the Problem
Adding Moq to the Visual Studio Project
Adding a Mock Object to a Unit Test
Creating a More Complex Mock Object
Summary
Chapter 7: SportsStore: A Real Application
Getting Started
Creating the Visual Studio Solution and Projects
Installing the Tool Packages
Adding References Between Projects
Setting Up the DI Container
Running the Application
Starting the Domain Model
Creating an Abstract Repository
Making a Mock Repository
Displaying a List of Products
Adding a Controller
Adding the Layout, View Start File and View
Setting the Default Route
Running the Application
Preparing a Database
Creating the Database
Defining the Database Schema
Adding Data to the Database
Creating the Entity Framework Context
Creating the Product Repository
Adding Pagination
Displaying Page Links
Improving the URLs
Styling the Content
Installing the Bootstrap Package
Applying Bootstrap Styles to the Layout
Creating a Partial View
Summary
Chapter 8: SportsStore: Navigation
Adding Navigation Controls
Filtering the Product List
Refining the URL Scheme
Building a Category Navigation Menu
Correcting the Page Count
Building the Shopping Cart
Defining the Cart Entity
Adding the Add to Cart Buttons
Implementing the Cart Controller
Displaying the Contents of the Cart
Summary
Chapter 9: SportsStore: Completing the Cart
Using Model Binding
Creating a Custom Model Binder
Completing the Cart
Removing Items from the Cart
Adding the Cart Summary
Submitting Orders
Extending the Domain Model
Adding the Checkout Process
Implementing the Order Processor
Registering the Implementation
Completing the Cart Controller
Displaying Validation Errors
Displaying a Summary Page
Summary
Chapter 10: SportsStore: Mobile
Putting Mobile Web Development in Context
Doing Nothing (Or As Little As Possible)
Using Responsive Design
Creating a Responsive Header
Creating a Responsive Product List
Creating Mobile Specific Content
Creating a Mobile Layout
Creating the Mobile Views
Summary
Chapter 11: SportsStore: Administration
Adding Catalog Management
Creating a CRUD Controller
Creating a New Layout
Implementing the List View
Editing Products
Creating New Products
Deleting Products
Summary
Chapter 12: SportsStore: Security & Finishing Touches
Securing the Administration Controller
Creating a Basic Security Policy
Applying Authorization with Filters
Creating the Authentication Provider
Creating the Account Controller
Creating the View
Image Uploads
Extending the Database
Enhancing the Domain Model
Creating the Upload User Interface Elements
Saving Images to the Database
Implementing the GetImage Action Method
Displaying Product Images
Summary
Chapter 13: Deployment
Preparing Windows Azure
Creating the Web Site and Database
Preparing the Database for Remote Administration
Creating the Schema
Deploying the Application
Summary
Chapter 14: Overview of MVC Projects
Working with Visual Studio MVC Projects
Creating the Project
Understanding MVC Conventions
Debugging MVC Applications
Preparing the Example Project
Launching the Visual Studio Debugger
Causing the Visual Studio Debugger to Break
Using Edit and Continue
Using Browser Link
Summary
Chapter 15: URL Routing
Preparing the Example Project
Creating the Example Controllers
Creating the View
Setting the Start URL and Testing the Application
Introducing URL Patterns
Creating and Registering a Simple Route
Using the Simple Route
Defining Default Values
Using Static URL Segments