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

Mastering ASP.NET Web API

Book Description

Leverage ASP.Net Web API to build professional web services and create powerful applications.

About This Book

  • Get a comprehensive analysis of the latest specification of ASP.NET Core and all the changes to the underlying platform that you need to know to make the most of the web API
  • See an advanced coverage of ASP.NET Core Web API to create robust models for your data, create controllers, and handle routing and security
  • This book is packed with key theoretical and practical concepts that can be instantly applied to build professional applications using API with Angular 4, Ionic, and React

Who This Book Is For

This book is for .Net developers who wants to Master ASP.NET Core (Web API) and have played around with previous ASP.NET Web API a little, but don’t have in-depth knowledge of it. You need to know Visual Studio and C#, and have some HTML, CSS, and JavaScript knowledge.

What You Will Learn

  • Acquire conceptual and hands-on knowledge of ASP.NET Core (MVC & Web API)
  • Learn about HTTP methods, the structure of HTTP content, internet media types, and how servers respond to HTTP requests and their associated HTTP codes
  • Explore middleware, filters, routing, and unit testing
  • Optimize Web API implementations
  • Develop a secure Web API interface
  • Deploy Web API projects to various platforms
  • Consume your web API in front end application based on Angular 4, Bootstrap, and Ionic
  • Implement and explore the current trends in service architecture

In Detail

Microsoft has unified their main web development platforms. This unification will help develop web applications using various pieces of the ASP.NET platform that can be deployed on both Windows and LINUX. With ASP.NET Core (Web API), it will become easier than ever to build secure HTTP services that can be used from any client.

Mastering ASP.NET Web API starts with the building blocks of the ASP.NET Core, then gradually moves on to implementing various HTTP routing strategies in the Web API. We then focus on the key components of building applications that employ the Web API, such as Kestrel, Middleware, Filters, Logging, Security, and Entity Framework.Readers will be introduced to take the TDD approach to write test cases along with the new Visual Studio 2017 live unit testing feature. They will also be introduced to integrate with the database using ORMs. Finally, we explore how the Web API can be consumed in a browser as well as by mobile applications by utilizing Angular 4, Ionic and ReactJS.

By the end of this book, you will be able to apply best practices to develop complex Web API, consume them in frontend applications and deploy these applications to a modern hosting infrastructure.

Style and approach

Using a hands-on approach, we cover both the conceptual as well as the technical aspects of the ASP.NET Core (Web API) framework.

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 code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Introduction to Microservices and Service-Oriented Architecture
    1. Services in SOA
      1. Service implementation
    2. Monolithic architecture
      1. Overheads of Monolithic architecture
    3. Introducing Microservices
      1. Lightweight yet scalable
      2. Technology agnostic
      3. Independently changeable
    4. Benefits of Microservices
    5. Summary
  3. Understanding HTTP and REST
    1. Software architecture
    2. REST principles
      1. Client - server
      2. Stateless
      3. Cache
      4. The uniform interface
      5. Layered system
      6. Code-on-demand
    3. REST architectural elements
      1. Data elements
      2. Resources and resource identifiers
      3. Representations
      4. Connectors
      5. Components
    4. HTTP
      1. HTTP/1.1 status codes
      2. An API example
      3. HTTP POST example
      4. HTTP GET example
      5. HTTP PUT example
      6. HTTP DELETE example
    5. Version 2 of HTTP
      1. Single connection
      2. Server push
      3. Multiplexing and streams
      4. Stream prioritization
    6. Binary messages
      1. Header compression
      2. Media types
    7. Richardson maturity model
      1. Level 0
      2. Level 1
      3. Level 2
      4. Level 3
    8. Summary
  4. Anatomy of ASP.NET Core Web API
    1. A quick recap of the MVC framework
    2. Inception of Web APIs and their evolution
      1. Introducing web API
      2. ASP.NET into Open Source world
    3. Introduction to .NET Core
      1. Install .NET Core SDK
        1. Creating and running a basic .NET Core application
    4. Introducing ASP.NET Core
      1. An overview of ASP.NET Core
    5. Creating ASP.NET Core Project using Visual Studio IDE
      1. Choosing the application type
      2. Selecting authentication type
    6. Creating ASP.NET Core web applications on Linux/macOS
    7. Creating ASP.NET Core web apps with Yeoman
      1. Creating ASP.NET Core Web API using Yeoman
      2. ASP.NET Core Web API Application structure
        1. Program.cs
        2. Startup.cs
        3. The Configure method
        4. The ConfigureServices method
        5. *.csproj
    8. ASP.NET Core request processing
      1. Unified MVC and Web API in ASP.NET Core
    9. Running the ASP.NET Core Web API project
      1. Running the application in Visual Studio IDE
      2. Running ASP.NET Core Web API on Linux/macOS
    10. Further reading
    11. Summary
  5. Controllers, Actions, and Models
    1. Introduction to controllers
    2. Actions
      1. Post
      2. Get
      3. Put
      4. Patch
      5. Delete
    3. Controllers
      1. Models
        1. Business
          1. Dependency Injection
      2. GET by ID
        1. Mapping
          1. Post
          2. Put
          3. Delete
          4. GetAll
    4. Summary
  6. Implementing Routing
    1. Introducing Routing
      1. Routing middleware
      2. The RouteBuilder class
      3. MapRoute
    2. ASP.NET Core Web API and Routing
    3. Convention-based Routing
    4. Template-based Routing
    5. Attribute-based Routing
      1. Attribute routes for RESTful applications
    6. Multiple Routes
    7. Routing constraints
      1. Types of route constraints
      2. Writing custom route constraints
    8. Link generation
    9. Routing best practices
    10. Summary
  7. Middleware and Filters
    1. Introducing middleware
    2. HTTP request pipeline and middleware
      1. Middleware in action
        1. Use()
        2. Run()
        3. Map()
        4. MapWhen()
    3. Order of middleware
    4. Built-in middleware
      1. Using StaticFiles middleware
    5. Writing custom middleware
    6. Migrating HTTP modules to middleware
    7. Introducing filters
      1. Filter pipeline
      2. Filter scoping
      3. Action filters
      4. Authorization filter
      5. Exception filter
    8. Summary
  8. Perform Unit and Integration Testing
    1. Uncle Bob's three rules of test-driven development
      1. Red-Green-Refactor
        1. Running the API test
        2. The Post Created test
        3. Setting the base URI
        4. The Post Conflict test
        5. The Put tests
        6. The Delete tests
      2. The xUnit tests
        1. The Models tests
          1. The validator class
        2. API Test
    2. Summary
  9. Web API Security
    1. Understanding Threat Model and OWASP
      1. Threat Model
      2. OWASP
    2. Applying SSL
    3. CORS
    4. Data Protection API
    5. Protecting web API
    6. Implementing JWT
      1. Generating JWTs
      2. Validating JWT
      3. OAuth
    7. Claims-based Authorizations
      1. Authorization using the claims policy
    8. Identity management in web API
      1. Adding the Identity package
      2. Configuring the Startup class
      3. Creating identity-related database
      4. Cookie-based authentication
      5. Two-factor authentication
    9. Summary
  10. Integration with Database
    1. Brief introduction to Object Relational Mapper
    2. Integrating ASP.NET Core Web API and an existing database using Entity Framework 6.x
      1. Restoring the AdventureWorks2014 database
      2. EF6 data access library
      3. Creating an ASP.NET Core app for the full .NET Framework
        1. Using IProductRepository to access the database
        2. Connection strings and IProductRepository in startup
        3. Using AutoMapper
        4. Writing ProductController to access the database
    3. Integrating using Dapper
      1. Creating AdvWrksDapper Web API and adding Dapper library
        1. Using IDepartmentRepository and department model to access the database
        2. Connection string and IOptions in ASP.NET Core
        3. Adding the DeparmentController Web API
    4. Integrating with EF Core
      1. Creating PacktContactsCore ASP.NET Core project
      2. Adding the EF Core package and tooling
      3. Contacts model class and DbContext
      4. Configuring services to use the SQL Server database
      5. EF tools for database migrations and updates
      6. ContactsController for CRUD operations
    5. Summary
  11. Error Handling, Tracing, and Logging
    1. Basics of Logging in ASP.NET Core
      1. Logging level
      2. Logging in action
      3. Logging category
      4. ILoggerFactory in dependency injection
    2. MyWallet - Demo ASP.NET Core project
    3. Logging errors to a file using NLog
    4. Logging errors to a database using Serilog
    5. Exception management in a MyWallet project
      1. Links to log management services
    6. Summary
  12. Optimization and Performance
    1. Measuring application performance
    2. Asynchronous controller action methods
    3. HTTP compression
      1. Adding response compression middleware
    4. Implementing in-memory caching
    5. Working with distributed caching
      1. Using an SQL Server distributed cache
    6. Response caching
    7. Summary
  13. Hosting and Deployment
    1. Creating a demo ASP.NET Core Web API project
    2. Publishing ASP.NET Core Web API project
      1. Publishing by CLI
      2. Publishing by Visual Studio Tooling
    3. Standalone web API
    4. Deployment strategy
    5. Deploying web API to IIS
      1. Configuring a website on IIS
    6. Deploying a web API to Azure App Service
    7. Publishing a web API to Windows virtual machine on Azure
    8. Publishing a web API to Docker
    9. Publishing a web API to EC2 on AWS
    10. Publishing a web API to Linux
    11. Testing PacktContacts web API
      1. Test case - Accessing a web API without the header
      2. Test case - Accessing a web API with the header
      3. Test case - Adding a contact to web API
      4. Test case - Getting a contact from the web API
      5. Test case - Getting all contacts from the web API
      6. Test case - Editing a contact to the web API
      7. Test case - Deleting a contact from the web API
    12. Summary
  14. Modern Web Frontends
    1. PacktContacts - Recap of the demo web API project
      1. Dealing with a cross-origin issue
    2. Software pre-requisites for web frameworks
    3. Consuming web APIs using Angular 4
      1. Angular CLI
        1. PacktServices - Angular Service Provider
        2. AppComponent - Angular Component
        3. The AppComponent template - HTML for Angular component
    4. Building hybrid mobile apps using Ionic 3
      1. Home page - Ionic 3 page
      2. Home page - Ionic 3 HTML page
    5. Building web apps using ReactJS
      1. ReactJS in ASP.NET web application
    6. Consuming web APIs using JavaScript
    7. Consuming web APIs using JQuery
    8. Summary