为 Web Api 版本控制自定义 MapHttpAttributeRoutes [英] Customize MapHttpAttributeRoutes for Web Api Versioning

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

问题描述

我正在实施 Web API 版本控制,如 Web 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

这一切正常,但控制器中的某些操作使用了 Route 属性

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

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

默认使用

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?

推荐答案

在官方 Codeplex 上的 WebApi 2.1 示例.它依赖于请求标头值来存储版本.

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-version"动态过滤控制器HTTP 标头.当路由使用约束时,每个约束都有一个阻止路由匹配给定请求的机会.在这示例中,自定义 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-version' 的值实现价值.提供了约束的允许版本的值通过放置在每个控制器上的 VersionedRoute 属性.当一个请求进来,'api-version' 的头值匹配预期的版本.此示例使用标题但使用约束实现可以使用任何标准来决定请求是否是对路线有效.

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天全站免登陆