簡體   English   中英

Web-API版本控制不適用於默認版本

[英]Web-API Versioning not working with Default version

我創建了一個帶有版本控制的Web API應用程序。 我將使用Microsoft.AspNet.WebApi.Versioning包來做到這一點。

Webapi配置:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.AddApiVersioning(o => {
            o.AssumeDefaultVersionWhenUnspecified = true;
        });
        // Web API routes
        config.MapHttpAttributeRoutes();

    }
}

在這里我的假設是如果我沒有通過該版本,默認情況下它將選擇版本1。

控制器代碼:

[ApiVersion("1.0")]
[RoutePrefix("api/users")]
public class UserV1Controller : BaseController
{
    public UserV1Controller()
    {
    }

    [Route("all", Name = "UsersCollection")]
    public async Task<IHttpActionResult> GetAll()
    {
        var items = await UnitOfWork.Users.GetPaged(x => x.OrderBy(y => y.Id), 1, 20);
        //Add mapping to DTO 
        return Ok(items);
    }

}

如果我使用http://localhost:10280/api/users/all?api-version=1.0 URL進行測試,它運行正常。 我將在現有項目中實現此功能。

為了向后兼容,我嘗試了http://localhost:10280/api/users/all URL。 它給我以下錯誤, 500作為狀態代碼。

{“消息”:“發生錯誤。”,
“ExceptionMessage”:“索引不能小於0或等於或大於集合中的項目數。\\ r \\ n
參數名稱:index \\ r \\ nActual value為0.“,
“ExceptionType”:“System.ArgumentOutOfRangeException”,
“StackTrace”:“at
Microsoft.Web.Http.Dispatcher上的Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.GetControllerName(HttpRequestMessage request)\\ r \\ n的System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.get_Item(Int32 index)\\ r \\ n。 1.CreateValue()\\r\\n at System.Lazy 1.CreateValue()\\ r \\ n中的ControllerSelectionContext。<> c__DisplayClass6_0。<。ctor> b__0()\\ r \\ n。在System.Lazy`1的1.CreateValue()\\r\\n at System.Lazy 1.LazyInitValue()\\ r \\ n中。的get_value(個)\\ r \\ n
在System.Web.Http.Dispatcher.HttpControllerDispatcher的Microsoft.Web.Http.Dispatcher.ConventionRouteControllerSelector.SelectController(ControllerSelectionContext context)\\ r \\ n at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.SelectController(HttpRequestMessage request)\\ r \\ n .d__15.MoveNext()“}


更新1:

經過討論和一些幫助后,我可以確認此默認版本適用於傳統路由。

使用屬性路由時再現該問題。 請檢查我的更新代碼。

  1. 我在WebAPIConfig文件中沒有任何默認的API路由。
  2. 我在控制器中更新Route Prefix和Route

現在問題轉載。

您還可以通過組合常規和屬性來重現該問題。

更新2:

現在我注意到問題在於配置。 如果我直接在Owin啟動類中添加路由配置,它可以正常工作。

    public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        var configuration = new System.Web.Http.HttpConfiguration();
        var httpServer = new System.Web.Http.HttpServer(configuration);

        // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
        configuration.AddApiVersioning(options =>
        {
            options.ReportApiVersions = true;
            options.AssumeDefaultVersionWhenUnspecified = true;
        });
        configuration.MapHttpAttributeRoutes();
        app.UseWebApi(httpServer);

    }
}

除了使用我們現有的WebAPIConfig類之外,它還會創建新的HttpConfiugration 所以我不知道它可能影響任何其他功能

因此,如果我將Owin配置為在Global.asax中使用webaPI而不是GlobalConfiguration.Configure(WebApiConfig.Register) ,那么它工作正常。

如果您未指定版本,則將其視為無版本。 在這種情況下,您必須設置AssumeDefaultVersionWhenUnspecified屬性,如果沒有另外說明,則假設默認版本為"1.0" ,如果沒有指定:

config.AddApiVersioning(o => {
    o.AssumeDefaultVersionWhenUnspecified = true;
});

此功能記錄在此處: https//github.com/Microsoft/aspnet-api-versioning/wiki/Existing-Services-Quick-Start

這是一個確認的錯誤,只有在托管在一個vanilla IIS實現(例如非OWIN)上時才會發生。 盡管HttpRouteCollection具有索引器,但System.Web定義的HostedHttpRouteCollection不支持它。 拋出通用的ArgumentOutOfRangeException而不是拋出NotSupportedException

有關更多詳細信息,請參閱: https//github.com/Microsoft/aspnet-api-versioning/issues/428

該修復程序已在,現在可在包3.0.1版中使用。

您可以嘗試設置默認版本號,並配置中間件以在未指定版本號時使用它。

參考: Scott關於路由的博客

services.AddApiVersioning(
    o =>
    {
        o.AssumeDefaultVersionWhenUnspecified = true );
        o.DefaultApiVersion = new ApiVersion("1.0");
    } );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM