Skip to main content
main-content

Über dieses Buch

Pro ASP.NET Web API shows you how to build flexible, extensible web services that run seamlessly on a range of operating systems and devices, from desktops to tablets to smart phones—even the ones we don’t know today.

ASP.NET Web API is a new framework designed to simplify web service architecture. So if you're tired of interoperability issues between inflexible web services and clients tied to specific platforms or programming languages, or if you've ever struggled with WCF, this book is for you.

To start with, you'll get up to speed on Web API's modern HTTP programming model, REST and your hosting options. You'll then dive into building a real application over a series of three chapters, so you can get an immediate feel for how this technology works in practice. The second half of the book features dedicated chapters on topics like routing, controllers, validation and tracing, and the authors close with discussions on performance, hosting and an all-important look at unit testing to help you prepare your application for the real world.

ASP.NET Web API makes HTTP a first-class citizen of .NET. With Pro ASP.NET Web API, you can build HTTP-based web services for your company or business, expose your data to the world across different formats and devices and gain the best possible global reach for your application.

Inhaltsverzeichnis

Frontmatter

Chapter 1. Introduction to ASP.NET Web API

The fact that you are reading this means you are interested in learning

something

about ASP.NET Web API (application programming interface). Perhaps you are relatively new to web services and want an expansive view. You may already be familiar with the technology and just need to swim a bit deeper into the Web API waters; hence, you need a beacon to guide you through unfamiliar places. Or maybe you are already a pro and just want to fill those few gaps in your knowledge. Wherever you are in this journey, stay tight as we are about to start our ride, and it is going to be fun!

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 2. Introduction to Asynchronous Programming in .NET

Asynchronous programming is generally one of the hardest programming concepts to grasp. It is even harder to understand what it means when it comes to dealing with server applications, notably web applications. Besides the general concept, the asynchronous programming model in the .NET Framework was also hard to grasp in the early days of the framework. With the introduction of Task Parallel Library (TPL) in .NET version 4.0, it became easier, but the programming model was still not ideal. With the new asynchronous programming language features of C# in .NET 4.5, it got easier still. It now may be said to offer the ideal programming model as well.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 3. HTTP , REST, and Hypermedia

Before speaking about ASP.NET Web API itself, we need to provide some fundamentals to get a better understanding of why ASP.NET Web API works as it does. The very basic goal of ASP.NET Web API was to make HTTP a first-class citizen in .NET and provide a foundation for RESTful applications based on the .NET Framework. So in order to understand the Web API framework better, we will explain the fundamentals of HTTP, REST and hypermedia, starting with HTTP basics.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 4. HttpClient

During the previous two chapters we’ve learned some important basics about asynchronous programming in .NET and the HTTP protocol. This is the first chapter that makes use of both foundations by introducing the

HttpClient

class; this class provides a way to handle HTTP requests and responses in .NET asynchronously on the client side (a server could also be a client). Before using

HttpClient

itself, let’s look at some new classes being used within both the

HttpClient

class and the ASP.NET Web API framework.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 5. Sample Application: Application Structure and Domain Model

There are two main approaches to learning new stuff: learning by practicing or learning the theory first. If the first one is your preferred option, the next three chapters are for you. If you prefer knowing theory first, you might want to skip to Chapter 8 and continue reading to Chapter 18 before coming back here. In the next three chapters I’ll help you build a sample application using ASP.NET Web API. Also, Chapter 7 will cover implementing a .NET wrapper around our HTTP API to make its consumption easy from .NET applications. We’ll then use this wrapper to build a client application based on ASP.NET MVC. However, the client application doesn’t have to be an ASP.NET MVC application. It could be an HTML5 application that makes AJAX calls to the HTTP API, a Windows Store application, or an iOS application. Any platform that knows how to talk HTTP can consume our HTTP API.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 6. Sample Application: Building the Web API

In Chapter 5, you set up the infrastructure of the HTTP API application. You created your database models by leveraging the Entity Framework code–first approach and created a generic repository to gain access to the data. You also introduced the service layer by implementing the membership and crypto services.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 7. Sample Application: Building the Wrapper and Web Client

So far, you have created your application’s domain layer and the API application with ASP.NET Web API framework. Because you now have open endpoints, you can build clients around the HTTP API to consume and modify data.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 8. Routing

In our ASP.NET Web API applications, incoming HTTP requests are handled by controller action methods. When an incoming request is received, it is routed to a proper action method, and the routing system provides the fundamental layer for this movement.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 9. Controllers and Actions

If we think of the ASP.NET Web API framework as a human body, the controller and actions part is the heart of that body. Many components of the Web API framework are glued together by the

ApiController

class, which is the default controller implementation provided out of the box. Filters, model binding, formatters, action selection—these are all part of the controller execution process.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 10. Message Handlers

When building an HTTP Application using ASP.NET Web API, you might face scenarios where the possibilities for processing and influencing the handling of requests and responses using the Routing module and the ApiController might not be enough. Furthermore there might be operations that you want to apply to more than one controller.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 11. Filters

In our Web API applications, we might have some special logic that needs to run at specific points in the request processing stage, yet implementing this logic in every single place that we want to run it can lead to an undesirable result in our code base. In the first place, this action breaks our DRY (Don’t Repeat Yourself) principle. Filters come into play at this point; they allow us to register our custom logic and have it run at specific points.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 12. Media Type Formatters and Model Binding

As you learned in Chapter 3, one of the six REST constraints is the uniform interface constraint. Important parts of the uniform interface are representations of the URIs that identify our application entities or state. You also saw in Chapter 3 that the representation formats are identified by Internet media types (or MIME types, as they were called in the past). As ASP.NET Web API aims to ease the implementation of HTTP or even more RESTful applications, it obviously needs to provide a way to create and read from different Internet media types without a hassle. How this is done in ASP.NET Web API will be explained in the course of this chapter.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 13. Input Validation

Regardless of the application type, input validation is a very important topic if the application accepts user inputs. As developers, we tend to assume that every user input is evil and they should be validated before being processed. Checking whether an input field is empty or not, validating the length of an input value, and comparing two inputs against each other according to a specific logic—these are just some of the common validation checks.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 14. Dependency Resolution

Nowadays you could hardly find serious software that does not use one or another form of dependency injection. ASP.NET Web API has been built with this in mind, and in fact it uses a dependency resolution model similar to ASP.NET MVC to resolve its public dependencies. This model allows you to plug in your dependency injection framework of choice.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 15. Unit Testing and Integration Testing

It is very likely you have already written unit tests for your projects and thus are familiar with the concepts, frameworks, and steps involved in writing unit tests. This chapter therefore mainly deals with how to apply those skills to the effective writing of unit tests for your Web API. As you will see, unit testing is where you start, not where you finish.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 16. Optimization and Performance

Having a fully functional HTTP API is the main concern when transmitting data over HTTP, but once lots of traffic starts to come in, you should realize that there might be bottlenecks and they could affect the performance of an application. Most concerns are application-specific, but there are some common scenarios and best practices that ought to be taken into consideration when you are building an application.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 17. Hosting

ASP.NET applications have historically used Internet Information Services (IIS) for their hosting, but during development, developers have used Visual Studio Development Server (also known as Cassini) or IIS Express. All in all, ASP.NET has been a web stack all the way and ASP.NET MVC did not change this.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Chapter 18. Tracing, Logging, and API Documentation

When an application is being developed, there are two things everyone expects it to provide, but almost nobody likes doing the work that providing them entails. We’re speaking of the implementation of tracing and logging features and the documentation of the application. Tracing and logging give the ability to write status or error logs or monitor the application state at runtime. Documentation of an application is mandatory, as the user needs guidance on how to work with an application. All these requirements exist independently of the type of application being developed. Documentation in the particular case of a public HTTP API is important, as nobody wants to explore the API by trial and error. This chapter will first focus on the tracing and logging features in ASP.NET Web API. The chapter’s second part is dedicated to the API documentation features of ASP.NET Web API.

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi

Backmatter

Weitere Informationen

Premium Partner

Neuer Inhalt

BranchenIndex Online

Die B2B-Firmensuche für Industrie und Wirtschaft: Kostenfrei in Firmenprofilen nach Lieferanten, Herstellern, Dienstleistern und Händlern recherchieren.

Whitepaper

- ANZEIGE -

Product Lifecycle Management im Konzernumfeld – Herausforderungen, Lösungsansätze und Handlungsempfehlungen

Für produzierende Unternehmen hat sich Product Lifecycle Management in den letzten Jahrzehnten in wachsendem Maße zu einem strategisch wichtigen Ansatz entwickelt. Forciert durch steigende Effektivitäts- und Effizienzanforderungen stellen viele Unternehmen ihre Product Lifecycle Management-Prozesse und -Informationssysteme auf den Prüfstand. Der vorliegende Beitrag beschreibt entlang eines etablierten Analyseframeworks Herausforderungen und Lösungsansätze im Product Lifecycle Management im Konzernumfeld.
Jetzt gratis downloaden!

Bildnachweise