簡體   English   中英

ASP.NET WebApi 版本控制不起作用

[英]ASP.NET WebApi versioning not working

我正在嘗試按照本教程實現 api 版本控制。 所以在我的啟動中,我有:

var constraintResolver = new DefaultInlineConstraintResolver()
{
    ConstraintMap =
    {
        ["apiVersion"] = typeof( ApiVersionRouteConstraint )
    }
};

configuration.MapHttpAttributeRoutes(constraintResolver);
configuration.AddApiVersioning()

和我的控制器:

[Route("api/v{version:apiVersion}/my")]
[ApiVersion("1.0")]
[ApiVersion("2.0")]
public class MyV1Controller 

[Route("api/v{version:apiVersion}/my")]
[ApiVersion("3.0")]
public class MyV3Controller 

當我請求http://localhost/api/v1.0/my 時出現錯誤

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.\r\n\r\nThe request has found the following matching controller types: \r\nMyV1Controller\r\nMyV2Controller

您能否建議如何使控制器版本控制工作?

我休息了一下,我記得在我的項目中我有一個自定義的 IHttpControllerSelector 實現,它擴展了 DefaultHttpControllerSelector。

configuration.Services.Replace(typeof(IHttpControllerSelector), new ApiControllerSelector(config));

在我刪除它之后,版本控制開始工作。 執行 configuration.AddApiVersioning 在 ServicesContainer 中設置 ApiVersionControllerSelector。 它不小心被我的自定義實現所取代。

暫無
暫無

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

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