0

我阅读这篇文章是为了尝试使用命名空间创建一个版本化的 webapi 控制器:

http://blogs.msdn.com/b/webdev/archive/2013/03/08/using-namespaces-to-version-web-apis.aspx

我不想破坏现有的路线,所以向后兼容对我来说非常重要。

如果路由不匹配,在尾注中使用默认路由听起来很简单,但这对我来说并不是微不足道的。如果你能帮忙,那就太棒了。

谢谢

4

1 回答 1

2

假设您使用的是文章中提到的代码,您可以在 SelectController 方法中执行此操作:

public HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
    IHttpRouteData routeData = request.GetRouteData();
    if (routeData == null)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }

    // Get the namespace and controller variables from the route data.
    string namespaceName = GetRouteVariable<string>(routeData, NamespaceKey);
    if (namespaceName == null)
    {
        DefaultHttpControllerSelector selector = new DefaultHttpControllerSelector(_configuration);
        return selector.SelectController(request);
    }

    // ... remaining code from article
}

当路由不包含版本参数时,创建 DefaultControllerSelector 的实例并从其 SelectController 方法返回值。

于 2013-09-18T09:49:14.803 回答