You are previewing Expert ASP.NET Web API 2 for MVC Developers.
O'Reilly logo
Expert ASP.NET Web API 2 for MVC Developers

Book Description

Web API 2 is the latest evolution of Microsoft's web services toolkit, which allows the creation of RESTful applications built on the ASP.NET platform. It provides a standards-based approach and a high-productivity development model that makes it easy to deliver services to a wide-range of clients, including mobile devices.

This book puts Web API into context for the experienced MVC Framework developer and dives deep into the tools and techniques required to build Web API applications that integrate with the MVC Framework and shows you how to create single-page applications to consume them.

Best-selling author Adam Freeman explains how to get the most from Web API 2 by building on the foundation of the MVC Framework and the ASP.NET platform. He starts with the nuts-and-bolts and shows you everything through to advanced features, going in-depth to give you the knowledge you need.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Part 1: Getting Ready
    1. Chapter 1: Getting Readys
      1. What Do You Need to Know?
      2. What Does Expert Mean?
      3. What Is the Structure of This Book?
        1. Part 1: Getting Ready
        2. Part 2: Results and Parameters
        3. Part 3: Dispatching Requests
      4. Are There Lots of Examples?
      5. Where Can You Get the Example Code?
      6. How Do You Set Up a Development Environment?
        1. Getting Visual Studio
        2. Getting Google Chrome
      7. Summary
    2. Chapter 2: Your First Web API Application
      1. Preparing the Example Project
        1. Creating the Visual Studio Project
        2. Adding and Updating NuGet Packages
        3. Setting the Port and Start URL
      2. Creating the MVC Application
        1. Creating the Model
        2. Creating the MVC Controller
        3. Creating the Views
        4. Using the MVC Application
      3. Creating the Web Service
        1. Creating the Web API Controller
        2. Testing the Web API Controller
      4. Implementing the Single-Page Client
        1. Setting Up JavaScript IntelliSense
        2. Defining the Client-Side Data Model and Controller
        3. Registering the JavaScript File
        4. Appling Data Bindings
        5. Testing the Single-Page Client
      5. Measuring the Single-Page Implementation
      6. Summary
    3. Chapter 3: Essential Techniques
      1. Preparing the Example Project
        1. Adding and Updating NuGet Packages
        2. Creating the Web API Controller
        3. Creating the MVC Framework Controller
        4. Setting the Port and Start URL
        5. Testing the Web Service
      2. Understanding Asynchronous Methods
        1. Understanding the Problem Asynchronous Methods Solve
        2. Implementing an Asynchronous Interface
        3. Creating a Self-Contained Asynchronous Method Body
        4. Returning a Task from a Synchronous Method Body
      3. Making Ajax Requests with jQuery
        1. Making an Ajax Request
        2. Understanding the $.ajax Method
      4. Using Knockout
        1. Applying the Bindings
      5. Summary
    4. Chapter 4: Understanding HTTP Web Services
      1. Understanding ASP.NET Web API
        1. Understanding Single-Page Applications
        2. Understanding Native Applications
        3. Understanding Shared-Model Applications
        4. Understanding Service Applications
      2. Understanding Simple Web Services
      3. Understanding RESTful Web Services
        1. Embracing HTTP
        2. Adding Data Discovery
      4. Summary
    5. Chapter 5: SportsStore: Preparation
      1. Preparing the Example Project
        1. Adding and Updating NuGet Packages
        2. Creating a Prep Controller
        3. Creating a Razor Layout
        4. Creating the OWIN Startup Class
        5. Setting the TCP Port
      2. Creating the Product and Order Models
        1. Defining the Model Classes
        2. Creating the Repository Classes
        3. Testing the Repository
        4. Checking the Database Schema
      3. Configuring ASP.NET Identity
        1. Defining the User and Role Classes
        2. Creating the Database Context Classes
        3. Creating the Manager Classes
        4. Adding the Configuration Statements
        5. Testing ASP.NET Identity
        6. Removing the Application Cookie
      4. Summary
    6. Chapter 6: SportsStore: A RESTful Application
      1. Creating a RESTful Web Service
        1. Testing the Products Web Service
      2. Putting the Web Service in Context
        1. Working with Regular C# Objects
        2. Using the RESTful Action Method Convention
      3. Configuring Serialization
      4. Adding Basic Data Validation
      5. Securing the Product Web Service
        1. Restricting Access to Action Method
        2. Authenticating Requests
      6. Adding Model Validation
        1. Applying Validation Attributes
        2. Validating the Model
      7. Adding Simple Dependency Injection
        1. Recapping the Problem
        2. Creating the Dependency Resolver
        3. Using the Dependency Resolver in the Controller Class
      8. Creating a Non-RESTful Web Service
        1. Preparing the Routing Configuration
        2. Preparing the Model Objects
        3. Preventing Formatting Loops
        4. Defining the Web API Controller
      9. Completing the Product Controller
      10. Summary
    7. Chapter 7: SportsStore: Creating the Clients
      1. Preparing the Example Project
        1. Setting Up JavaScript IntelliSense
        2. Updating the Layout
      2. Implementing the Common Code
        1. Defining the Ajax Layer
        2. Defining the Model
        3. Defining the Authentication Controller
        4. Defining the Products Controller
        5. Defining the Orders Controller
      3. Creating the Customer Client
        1. Creating the Customer Model
        2. Creating the Customer Controller
        3. Creating the Views
      4. Creating the Administration Client
        1. Creating the Admin Model
        2. Creating the Admin Controller
        3. Creating the Views
        4. Testing the Admin Client
      5. Summary
    8. Chapter 8: SportsStore: Deployment
      1. Preparing the SportsStore Application
        1. Preventing the Product Database from Resetting
        2. Adding Database Connection Strings
      2. Preparing Azure
        1. Creating the Databases
        2. Creating the Web Site
        3. Downloading the Publish Profile
      3. Deploying the Application
        1. Configuring the Databases
      4. Publishing the Application
      5. Summary
  10. Part 2: Results and Parameters
    1. Chapter 9: The Anatomy of ASP.NET Web API
      1. Understanding the Web API Namespaces and Types
      2. Understanding the Web API Context Objects
        1. Getting Information About the Request
        2. Getting Information About the Controller
        3. Getting Information About Everything Else
      3. Understanding the Web API Components
        1. Application Configuration
        2. Controllers, Actions, and Results
        3. Services
        4. Dispatchers and Handlers
      4. Summary
    2. Chapter 10: Creating and Configuring a Web API Application
      1. Preparing the Example Project
        1. Creating the Model and Repository
        2. Creating an HTTP Web Service
        3. Creating the Browser Client
        4. Testing the Example Application
      2. Configuring a Web API Application
        1. Configuring Web API Through the ASP.NET Platform
        2. Understanding the Configuration Object
      3. Configuring Web API Dependency Injection
        1. Preparing for Dependency Injection
        2. Understanding the Web API Dependency Interfaces
        3. Installing the Dependency Injection Container
        4. Implementing the Dependency Interfaces
        5. Configuring Web API
      4. Configuring Dependency Injection for Web API and MVC
        1. Declaring the Dependency
        2. Installing the Dependency Injection Packages
        3. Adding MVC Support to the Resolver
        4. Configuring the MVC Framework
      5. Summary
    3. Chapter 11: Action Method Results
      1. Preparing the Example Project
      2. Understanding Action Method Results
      3. Returning No Result
        1. Consuming a No Result Action Method
      4. Returning an Action Result
        1. Understanding the IHttpActionResult Interface
        2. Using the ApiController Action Result Methods
        3. Returning Other Status Codes
        4. Creating a Custom Action Result
      5. Returning Model Data
        1. Understanding the Default Behavior
        2. Understanding the Content Negotiation Process
        3. Implementing a Custom Content Negotiator
        4. Bypassing Content Negotiation
      6. Returning Negotiable Action Results
        1. Creating Negotiable Action Results
      7. Summary
    4. Chapter 12: Creating Media Type Formatters
      1. Preparing the Example Project
      2. Creating a Media Type Formatter
        1. Implementing a Basic Media Type Formatter
      3. Refining the Custom Formatter
        1. Supporting Content Encodings
        2. Setting the HTTP Response Headers
      4. Participating in the Negotiation Process
        1. Creating a Media Type Mapping
        2. Using the Mapping Extension Methods
      5. Creating Per-Request Media Type Formatters
        1. Creating the Formatter Instance
        2. Testing the Per-Request Formatter
      6. Summary
    5. Chapter 13: Using the Built-in Media Formatters
      1. Preparing the Example Project
      2. Working with the Built-in Media Type Formatters
        1. Listing the Built-in Media Type Formatters
        2. Dealing with Type Matching During Negotiation
      3. Working with the JSON Media Type Formatter
        1. Configuring the JSON Media Type Formatter
        2. Configuring Json.Net
      4. Using the XML Media Type Formatter
        1. Configuring the XML Media Type Formatter
        2. Getting the Xml Media Type Formatter Working
      5. Summary
    6. Chapter 14: Understanding Parameter and Model Binding
      1. Preparing the Example Project
        1. Creating the Controller
        2. Creating the Client
        3. Adding a New Route
        4. Testing the Example Application
      2. Understanding the Default Binding Behavior
        1. Understanding Parameter Binding
        2. Understanding Model Binding
      3. Performing Binding Customizations
        1. Binding Complex Types from the Request URL
        2. Binding Simple Types from the Request Body
        3. Defining a Binding Rule
      4. Manually Obtaining Request Values
        1. Handling POST Requests
      5. Summary
    7. Chapter 15: Binding Simple Data Types
      1. Preparing the Example Project
      2. Preparing the Common Code
      3. Working with Value Providers and Value Provider Factories
        1. Understanding Value Providers and Value Provider Factories
        2. Creating a Custom Value Provider and Factory
      4. Applying a Custom Value Provider and Factory
        1. Understanding How Web API Looks for Values
        2. Applying a Value Provider Factory with an Attribute
        3. Extending the Default Behavior
        4. Creating a Parameter Binding Rule
      5. Summary
    8. Chapter 16: Binding Complex Data Types Part I
      1. Preparing the Example Project
      2. Using the Built-in Model Binders
        1. Binding Objects
        2. Broadening the Source of Binding Values
        3. Binding Collections and Arrays
        4. Binding Key-Value Pairs
      3. Working with Custom Model Binders
        1. Preparing the Application
        2. Understanding Model Binders
        3. Creating a Custom Model Binder
        4. Applying a Custom Model Binder
      4. Using Type Converters
        1. Understanding Type Converters
        2. Creating a Type Converter
        3. Applying a Type Converter
      5. Summary
    9. Chapter 17: Binding Complex Data Types Part II
      1. Preparing the Example Project
        1. Testing the Application
      2. Creating a Custom Media Type Formatter
        1. Preparing the Client
        2. Creating the Media Type Formatter
        3. Registering and Testing the Media Type Formatter
      3. Using the Built-in Media Type Formatters
        1. Handling URL-Encoded Data
        2. Handling JSON Requests
        3. Handling XML Requests
      4. Customizing the Model Binding Process
        1. Changing the Behavior of the Default Action Value Binder
        2. Creating a Custom Action Value Binder
      5. Summary
    10. Chapter 18: Model Validation
      1. Preparing the Example Project
        1. Testing the Changes
      2. Understanding Common Data Problems
        1. Understanding Under-Posting
        2. Understanding Over-Posting
        3. Understanding Bad Data
      3. Using Web API Model Validation
        1. Understanding Model State
        2. Testing the Model State
        3. Removing the Debug Output Code
      4. Using the Binding Control Attributes
      5. Performing Validation with Validation Attributes
        1. Using the Built-in Validation Attributes
      6. Creating a Self-validating Model Class
      7. Performing Validation in a Media Type Formatter
        1. Creating a Validating Media Type Formatter
        2. Registering and Using the Custom Media Type Formatter
      8. Summary
  11. Part 3: Dispatching Requests
    1. Chapter 19: Dispatching Requests
      1. Preparing the Example Project
        1. Creating the Model Class
        2. Creating the Web API Web Service
        3. Creating the MVC Controller and View
        4. Testing the Example Application
      2. Understanding Request Dispatching
        1. Understanding the HttpServer Class
        2. Understanding the HttpRoutingDispatcher Class
        3. Understanding the HttpControllerDispatcher Class
      3. Customizing the Dispatch Process
        1. Creating Custom Message Handlers
        2. Customizing Other Dispatch Components
      4. Summary
    2. Chapter 20: URL Routing: Part I
      1. Preparing the Example Project
        1. Testing the Application Changes
      2. Understanding URL Routing
        1. Understanding the Routing Classes and Interfaces
      3. Working with Convention-Based Routing
        1. Using Route Templates
        2. Routing to the New Controller
      4. Controlling Route Matching
        1. Using Routing Data Default Values
        2. Using Routing Constraints
      5. Summary
    3. Chapter 21: URL Routing: Part II
      1. Preparing the Example Project
      2. Understanding Direct Routing
        1. Creating a Direct Route
        2. Creating a Controller-wide Direct Route
      3. Customizing URL Routing
        1. Using a Route-Specific Message Handler
        2. Applying Custom Constraints to Direct Routes
      4. Summary
    4. Chapter 22: Controllers and Actions
      1. Preparing the Example Project
      2. Understanding Controllers
        1. Creating a Controller
        2. Using Built-in Services and Features
      3. Understanding the ApiController Dispatch Process
        1. Preparing the Example Controller
        2. Understanding the Action Selection Process
        3. Understanding the RESTful/Non-RESTful Routing Problem
        4. Understanding Filters
        5. Understanding the Action Method Execution Process
      4. Customizing the Controller Dispatch Process
        1. Creating a Custom IHttpActionInvoker Implementation
        2. Creating a Custom IHttpActionSelector Implementation
        3. Creating a Controller-Specific Configuration
      5. Summary
    5. Chapter 23: Filters Part I
      1. Preparing the Example Project
      2. Understanding Filters
      3. Working with Action Filters
        1. Creating an Action Filter by Implementing IActionFilter
        2. Using the Convenience Action Filter Base Class
        3. Creating a Short-Circuiting Action Filter
      4. Understanding the Filter Pipeline
        1. Displaying the Filter Pipeline
        2. Understanding Filter Scope
      5. Working with Authentication Filters
        1. Preparing for Authentication
        2. Understanding Authentication Filters
        3. Creating an Authentication Filter
        4. Viewing the Filter Pipeline
      6. Summary
    6. Chapter 24: Filters Part II
      1. Preparing the Example Project
      2. Reviewing Filters in the Dispatch Process
      3. Working with Authorization Filters
        1. Understanding Authorization Filters
        2. Creating an Authorization Filter
        3. Appling the Authorization Filter
        4. Removing the Authentication Filter
        5. Using the Built-in Authorization Filter Attributes
        6. Reworking the Authentication Filter
      4. Working with Exception Filters
        1. Understanding the Default Behavior
        2. Understanding Exception Filters
        3. Creating an Exception Filter
        4. Deriving the Filter from the ExceptionFilterAttribute Class
      5. Working with Override Filters
        1. Overriding Built-in Filter Types
        2. Redefining Filter Policies
      6. Summary
    7. Chapter 25: Error Handling
      1. Preparing the Example Project
      2. Dealing with Errors
        1. Relying on the Default Behavior
        2. Using an Implementation of the IHttpActionResult Interface
      3. Using the HttpError Class
        1. Using an Error Response and an HttpError Object
        2. Adding Extra Information to the HttpError Object
        3. Including Model State Errors in the HTTP Response
        4. Controlling Error Detail
        5. Displaying HttpError Information in the Client
      4. Responding to Errors Globally
        1. Handling Exceptions
        2. Logging Exceptions
      5. Summary
    8. Chapter 26: Using OWIN
      1. Understanding OWIN
      2. Creating a Self-hosted Web API Application
        1. Creating the Project
        2. Installing the Packages
        3. Creating the Model and Repository
        4. Defining the Configuration Classes
        5. Creating the Web API Controller
        6. Testing the Self-hosted Web API Application
      3. Summary
  12. Index