• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 12 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Versioning support for ASP.NET Web Api

SDammann.WebApi.Versioning

Easy, well-tested, and extendable ASP.NET Web API versioning.

There is also a Nuget package available.

Using the library

Setting up

Register the DefaultControllerIdentificationDetector and DefaultRequestControllerIdentificationDetector class with your dependency container.

Next, choose a versioning scheme and register it with the library. For example, to use versioning by API route (this will use urls like /api/v1.2/product/) include the following in your Web API configuration delegate:

ApiVersioning.Configure(config)
             .ConfigureRequestVersionDetector<DefaultRouteKeyVersionDetector>();

The code assumes the config variable is of type HttpConfiguration. Make sure to register this class with your dependency container.

Choosing an API versioning scheme

Currently the library supports two API versioning schemes.

API versions by URL / routing.

This will allow the API version to be chosen by your URL routing scheme. Example:

config.Routes.MapHttpRoute(
	name: "DefaultApi",
	routeTemplate: "api/v{version}/{controller}/{id}",
	defaults: new { id = RouteParameter.Optional }
);

ApiVersioning.Configure(config)
             .ConfigureRequestVersionDetector<DefaultRouteKeyVersionDetector>();

API versions by HTTP header

A scheme preferred by many REST purists. This allows the API version to be chosen by using the MIME type in the Accept header of the request. In order to achieve this, inherit from AcceptHeaderRequestVersionDetector and implement the GetVersionFromSingleHeader method.

Next, register your implementation in the Web API configuration delegate:

ApiVersioning.Configure(config)
             .ConfigureRequestVersionDetector<YourCustomRoutingDetector>();

Don't forget to set-up your custom MIME-type in content negotiation so ASP.NET Web API will output the correct response format.

Concepts

The library distinguishes several responsibilities in API versioning:

  • Controller naming / versioning
  • External request naming / versioning
  • The definition of a version itself

Controller name and version detection are implemented using the IControllerNameDetector and IControllerVersionDetector interfaces. The default DefaultControllerNameDetector implementation uses the ASP.NET Web API conventions, like requiring your controller class name to end with Controller. The default DefaultControllerVersionDetector expects you to put your controllers in 'versioned' namespaces. For instance, controllers in namespace MyApi.Version1_1 will point to version 1.1 of the API. There is also an implementation available that allows you to use attributes instead (DefaultAttributeControllerVersionDetector).

Request controller naming and versioning is implemented using the IRequestControllerNameDetector and IRequestVersionDetector interfaces. The default DefaultRequestControllerNameDetector implementation uses the ASP.NET Web API conventions. There is no default IRequestVersionDetector configured. The library forces you to choose an API versioning scheme.

An API version itself is abstracted away as an ApiVersion. This is an abstract class that allows the concept of versioning itself to be customizable. For example, the SemVerApiVersion uses a one to four numbers to be used for API versions. One could easily extend this to support letters, for example as designation for 'beta' and 'alpha' by inheriting from SemVerApiVersion and then implement IControllerVersionDetector and IRequestVersionDetector or extend the existing implementations.

Advanced concepts

Custom exception handling

By default the library will properly handle exceptions that occur during controller selection itself. In some cases you may want to override or modify the error messages returned to the client by this library. You may do this in two ways:

  1. Implementing a global exception handler and handle any BaseApiException derived exception yourself, as per ASP.NET Web API documentation. Note that the library registers it's own exception handler, but you may wish to use your own. In that case you can always delegate any exception handling to IVersioningExceptionHandler.

  2. Implement a custom IVersioningExceptionHandler. This exception handler is called by the library when a API versioning error occurs. You can implement your own custom response here. You can also inherit or delegate to DefaultVersioningExceptionHandler if you can't or won't handle an exception in certain cases.

Extending the identification of a controller

By default, a Web API controller is identified by its version and name (in the form of a ControllerIdentification instance). It is possible to extend or modify this behavior. This is done by inheriting from the ControllerIdentification class and implementing custom versions of the IControllerIdentificationDetector and IRequestControllerIdentificationDetector interfaces or extending the default implementations.

Note this is usually not necessary, but if you'd like to go beyond names and versions you can use this approach.

Contact

Any questions or remarks? Drop me a twitter message (@sebazzz91) or put an issue in the issue tracker.

Older version

The legacy older version (2.x) is available at the 2.x branch.

License

Apache 2.0 license. License details are available in the LICENSE file.

More Repositories

1

Return

Realtime retrospective tool built in ASP.NET Core and Blazor
C#
74
star
2

financial-app

Financial management SPA app created using ASP.NET Core, knockout.js and Typescript
C#
50
star
3

EntityProfiler

An open-source initiative for creating an Entity Framework query profiler
C#
31
star
4

PokerTime

Planning poker application written in ASP.NET Core Blazor
C#
26
star
5

IFS

Internet File Store - Small scale private upload service
C#
11
star
6

SystemWebpack

Webpack dev server and hot module replacement support for System.Web / ASP.NET projects
C#
10
star
7

VsRemovePublishButton

Remove the "Publish" button in the Visual Studio status bar. Useful for when you're already using a different version control system.
C#
6
star
8

NUnitTestOrdering

Allows to use hierarchical (integration) test ordering in NUnit and supports workflow based testing
C#
5
star
9

SDammann.T4WPF

Strongly-typed navigation page generator T4-file
C#
4
star
10

NetUnitTestComparison

Comparison between MSTest, NUnit 3 and XUnit 2
C#
3
star
11

sebazzz.github.io

Site generation of my personal home page
G-code
2
star
12

SDammann.WindowsPhone.Utils

Useful utility functions and extensions for Windows Phone
C#
1
star
13

SDammann.WindowsPhone.MyMvvm

A simple and useful MVVM framework that supports dependency injection
C#
1
star
14

fiddlerfox

Allows you to point Firefox to your fiddler instance if it doesn't refresh the proxy settings after Fiddler has started
JavaScript
1
star
15

LocaltunnelClient

.NET Core implementation of a localtunnel client (library and CLI)
C#
1
star
16

typescript-knockout-to-json-mapping

Typescript Knockout to JSON mapping
JavaScript
1
star
17

BIQUB1-Marlin

My BIQU B1 Marlin build & settings
C++
1
star
18

sdos

Damsteen Operating System - Some fiddling with assembly and C
Assembly
1
star
19

lsbasi

Me trying "Let's build an interpreter" in C++
C++
1
star