O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ASP.NET Web API 2: Building a REST Service from Start to Finish

Book Description

The ASP.NET MVC Framework has always been a good platform on which to implement REST-based services, but the introduction of the ASP.NET Web API Framework raised the bar to a whole new level.Now in release version 2.1, the Web API Framework has evolved into a powerful and refreshingly usable platform. This concise book provides technical background and guidance that will enable you to best use the ASP.NET Web API 2 Framework to build world-class REST services.

New content in this edition includes:

• New capabilities in Web API 2 (currently version 2.1).

• Support for partial updates, or PATCH.

• API versioning.

• Support for legacy SOAP-based operations.

• How to handle non-resource APIs using REST

• How to best expose relationships between resources

• JSON Web Tokens, CORS, CSRF

Get ready for authors Jamie Kurtz and Brian Wortman to take you from zero to REST service hero in no time at all. No prior experience with ASP.NET Web API is required; all Web API-related concepts are introduced from basic principles and developed to the point where you can use them in a production system. A good working knowledge of C# and the .NET Framework are the only prerequisites to best benefit from this book.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Authors
  8. About the Technical Reviewers
  9. Acknowledgments
  10. Foreword
  11. Introduction
  12. Chapter 1: ASP.NET as a Service Framework
    1. In the Land of JavaScript and Mobile Devices
    2. Advantages of Using the ASP.NET Web API
      1. Configuration
      2. REST by Default
      3. Abstraction with Routes
      4. Controller Activation Is, Well, Very Nice
      5. Simpler Extensible Processing Pipeline
      6. Interoperability of JSON, XML, and REST
    3. A Few Feature Highlights of the ASP.NET Web API
    4. Summary
  13. Chapter 2: What Is RESTful?
    1. From RPC to REST
      1. XML-RPC and SOAP
      2. URIs and Resources
      3. HTTP Verbs
      4. HATEOAS
    2. HTTP Status Codes
    3. HAL, Collection+JSON
    4. Summary
  14. Chapter 3: Designing the Sample REST API
    1. Task Management Resource Types
      1. Hypermedia Links
      2. Modeling the URIs and HTTP Verbs
    2. The Task-Management Data Model
    3. Choosing Architecture Components
      1. Data Access
      2. Type Mapper
      3. IoC Container
      4. Logger
      5. Testing Framework
      6. Mocking Framework
    4. Summary
  15. Chapter 4: Building the Environment and Creating the Source Tree
    1. Configuring the Machine
      1. Windows 8 64-bit with .NET Framework 4.51
      2. SQL Server 2012
      3. Visual Studio 2013
      4. NuGet Package Manager 2.6
    2. Creating the Folder Structure
    3. Creating the Solution
    4. NuGet Config File
    5. Adding the Projects
    6. Basic Components
      1. Domain Model
      2. Service Model Types
      3. Logging
      4. The Database
    7. Summary
  16. Chapter 5: Up and Down the Stack with a POST
    1. Routing
      1. Adding an HttpRequestMessage Argument
      2. Adding a Model Object Argument
      3. Attribute-Based Routing
    2. Versioning
      1. Implementing POST
    3. Dependencies
      1. Constructor Injection of Dependencies
      2. Configuring Ninject Dependency Injection
      3. Container Configuration
      4. Container Bindings
      5. IDependencyResolver for Ninject
      6. Completing NinjectWebCommon
    4. NHibernate Configuration and Mappings
      1. Database Configuration: Overview
      2. Adding Concurrency Support to Entities
      3. Entity Mapping
      4. Mapping Relationships
      5. Database Configuration: Bringing It All Together
    5. Managing the Unit of Work
    6. Database Transaction Control
    7. Diagnostic Tracing
    8. Error Handling
    9. Persisting a Task and Returning IHttpActionResult
      1. New Service Model Type
      2. Persisting the Task
      3. IHttpActionResult
    10. Summary
  17. Chapter 6: Securing the Service
    1. The Main Idea
      1. Authentication
      2. Authorization
      3. Overview of the Authentication and Authorization Process
    2. Securing the POST
      1. The Authorization Filter
      2. A Message Handler to Support HTTP Basic Authentication
    3. Securing Non-Resource API Operations
      1. Activate a Task
      2. Complete a Task
      3. Reactivate a Task
      4. Auditing
    4. GET a Task
    5. Applying Token-Based Security
      1. Token Basics
      2. The JSON Web Token
      3. Configuring the JwtAuthForWebAPI Package
      4. Getting a Task Using a JWT
    6. SSL, XSS, CORS, and CSRF
    7. Summary
  18. Chapter 7: Dealing with Relationships, Partial Updates, and Other Complexities
    1. Task and User Relationships
    2. Partial Update of a Task Using PUT/PATCH
    3. Validation Using an Action Filter
      1. Specialized Action Filter to Validate Task Updates
      2. Generalized Action Filter to Validate New Tasks
    4. Paging of Results
      1. Constructing the Filter with a Data Request Factory
      2. Filtering the Results
    5. Hypermedia Links
      1. Common Link Service
      2. Business Domain-Specific Link Services
      3. Putting It Together
    6. Summary
  19. Chapter 8: Supporting Diverse Clients
    1. Project Requirements
    2. Content Negotiation
    3. Supporting SOAP-Based Clients
      1. Where Is the Controller?
      2. Configuring the Route
      3. Adding a Custom Formatter
    4. Summary
  20. Chapter 9: Completing the Picture
    1. Testing the API
      1. Unit Testing
      2. Integration Testing
    2. Going Live!
      1. Logging In
      2. Support for CORS
    3. Summary
  21. Index