Configuring Web API

You may have been wondering about the Configuration property on the controller. In traditional ASP.NET applications, application configuration is done in Global.asax, and the application uses global state (including statics and thread local variables) to give access to the request and application configuration.

Web API was designed not to have any such static global values, and instead put its configuration into the HttpConfiguration class. This has two impacts on application design: First, you can run multiple Web API servers in the same application (since each server has its own non-global configuration); second, you can run both unit tests and end to end tests more easily in Web API, since you contain that configuration into a single non-global object, as statics make parallelized testing much more challenging. The configuration class includes access to the following items:

  • Routes
  • Filters to run for all requests
  • Parameter binding rules
  • The default formatters used for reading and writing body content
  • The default services used by Web API
  • A user-provided dependency resolver for DI on services and controllers
  • HTTP message handlers
  • A flag for whether to include error details like stack traces
  • A Properties bag which can hold user-defined values

How you create or get access to this configuration depends on how you are hosting your application: inside ASP.NET or inside WCF self-host.

Configuration in Web-Hosted Web API

The default MVC project templates are all ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.