O'Reilly logo

Professional ASP.NET MVC 4 by Scott Hanselman, K. Scott Allen, Brad Wilson, Phil Haack, Jon Galloway

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

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

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

Start Free Trial

No credit card required