自定义的Web API版本MapHttpAttributeRoutes [英] Customize MapHttpAttributeRoutes for Web Api Versioning

查看:1884
本文介绍了自定义的Web API版本MapHttpAttributeRoutes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现Web API版本如网站API版本。我的控制器有2个独立的命名空间,我已经使用了自定义SelectController方法来选择使用基于一个查询参数的版本。
例如

I am implementing Web API versioning as in Web API Versioning. My controllers are in 2 separate namespaces, and I've used a custom SelectController method to choose which version to use based on a a query parameter. e.g.

http://myapi/api/values?version=1.0

这一切工作正常,但在控制器中的一些动作使用路由属性

This all works fine but some actions in the controllers use the Route attribute

[Route("api/values/getNames")]
public HttpResponseMessage Get() { ... }

其中使用的是默认情况下映射到正确的控制器

Which are mapped to the correct controller by default using

config.MapHttpAttributeRoutes();

在WebApiConfig.cs

in WebApiConfig.cs

如果我有同样的路线API的多个版本,这是行不通的。我能够提供config.MapHttpAttributeRoutes自定义实现(),所以我可以选择使用该API的正确版本,或者是有这样做的更好的办法?

This will not work if I have multiple versions of the API with the same route. Am I able to provide a custom implementation for config.MapHttpAttributeRoutes() so I can select the correct version of the API to use, or is there a better way of doing this?

推荐答案

有一个例子这个在官方的WebApi 2.1 examplex在codePLEX
它依赖于请求头值来存储的版本。

There is an example for this in the official WebApi 2.1 examplex on Codeplex. It relies on a request header value to store the version.

我认为这是一个要好很多,因为它可以让路由保持所有版本相同。客户端简单地通过包括在请求的HTTP标头(在此情况下,版本号)选择的版本。

I think it's a lot nicer, since it allows the routes to stay the same for all versions. Clients select the version simply by including an HTTP header in the request (in this case the version number).

此示例演示如何使用属性路由和约束
  的ASP.NET Web API由一个API版本的动态过滤控制器
  HTTP标头。当路由使用约束,每个约束都有
  机会prevent从匹配给定的请求的路线。在这
  样,定制RouteFactoryAttribute(VersionedRoute)增加了一个
  约束每个属性的路线。

This sample shows how to use Attribute Routing and Constraints in ASP.NET Web API to dynamically filter controllers by an 'api-version' HTTP header. When a route uses Constraints, each constraint has a chance to prevent the route from matching a given request. In this sample, a custom RouteFactoryAttribute (VersionedRoute) adds a constraint to each attribute route.

...

自定义约束实现(VersionConstraint)是
  实现了基于的API版本'匹配的整数值
  值。设置的允许版本为约束的值
  由放置在每个控制器上VersionedRoute属性。当一个
  请求到来的API版本头值与匹配
  预期的版本。此示例使用标题,但约束
  实施可以使用任何标准来决定,如果该请求是
  有效的路由。

The custom constraint implementation (VersionConstraint) is implemented based on the value of 'api-version' matching an integer value. The value of the allowed version for the constraint is provided by the VersionedRoute attribute placed on each controller. When a request comes in, the header value of 'api-version' is matched against the expected version. This example uses a header but a constraint implementation could use any criteria to decided if the request is valid for the route.

不管怎样,最终的结果最终会这样看:

Anyway, the end result would end up looking like this:

[VersionedRoute("api/Customer", 1)]
public class CustomerVersion1Controller : ApiController
{
    // controller code goes here
}
[VersionedRoute("api/Customer", 2)]
public class CustomerVersion2Controller : ApiController
{
    // controller code goes here
}

这篇关于自定义的Web API版本MapHttpAttributeRoutes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆