Microsoft.AspNet.WebApi 5.2.7路由问题

时间:2019-02-01 11:19:40

标签: asp.net .net asp.net-web-api routing

我们在项目中使用了Microsoft.AspNet.WebApi 5.2.3,最近我们已升级到5.2.7,并且某些现有功能停止工作:

我有这个FileController:

[ApiVersion("1")]
[RoutePrefix("api/v{version:apiVersion}/file")]
public class FileController : BaseController
{
    [HttpPost]
    [MapToApiVersion("1")]
    [Route("uploadmethod"), Obsolete("The action is marked as obsolete for missing the support for userId. Please use uploadmethod from version 2.")]
    public async Task<HttpResponseMessage> UploadFileStream([FromUri] string filePath = null...)
    {

    }
}

然后是从上述控制器派生的另一个控制器:

[ApiVersion("2")]
[RoutePrefix("api/v{version:apiVersion}/file")]
public class FileController : Controllers.FileController
{
    [HttpPost]
    [MapToApiVersion("2")]
    [Route("uploadmethod")]
    public async Task<HttpResponseMessage> UploadFileWithUserId([FromUri] string userId = null, [FromUri] string filePath = null)
    {
       //at some point here I call an internal method from the base FileController
    }
}

使用5.2.3时,以下代码可以正常工作:

$"api/v1/file/uploadmethod?filePath={path}";

现在,升级到5.2.7后,我收到此错误: “找到了多个与URL匹配的控制器类型。如果多个控制器上的属性路由与请求的URL匹配,则可能会发生这种情况。”

我需要MapToApiVersion属性,因为我需要在下面进行测试才能成功。

    [Fact]
    public async Task UploadFileUsingVersion2NUserId_WillThrowError_MethodsIsMissingOnThatVersion()
    {       
            //...
            var url = $"api/v2/file/uploadmethod?filePath={path}";

            // When
            var response = await api.UploadFile(url, content);

            // Then
            Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
            var errorResponse = await response.Content.ReadAsJsonAsync<UnsupportedVersionResponse>();
            Assert.Equal("UnsupportedApiVersion", errorResponse.Error.Code);
    }

发生了什么变化?如何仅对基本控制器强制执行v1?

0 个答案:

没有答案