gpt4 book ai didi

c# - 是否可以在 Controller 的 ApiVersion 属性中包含下划线?

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:00 24 4
gpt4 key购买 nike

我继承了一个遗留的 WebAPI 系统,该系统目前在路由模式中使用下划线来表示版本。例如/api/1_0/account, /api/1_1/account

我正在尝试更新自动生成的文档以使用 Swagger,但是使用包含下划线的 ApiVersion 属性的显式路由会导致异常。例如,这很好用:

[ApiVersion("1")]

但是这会引发异常:

[ApiVersion("1_0")] // < note '_0' here
[RoutePrefix("api/{version:apiVersion}/account")]
public class AccountController : ApiBaseController
{
// actions...
}

异常(exception)情况是:

FormatException: The specified API version status '_1' is invalid.
System.InvalidOperationException: 'Failed to compare two elements in the array.'
at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.InitializeControllerInfoCache()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.GetControllerMapping()
at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass1_1.b__1()
at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass1_0.b__0(HttpConfiguration config)
at System.Web.Http.HttpConfiguration.EnsureInitialized()
at ProjectName.Startup.Configuration(IAppBuilder app) in E:\ProjectPath\Foo.cs:line 25

问题很明显,但是如何在版本属性值中包含下划线呢?这个问题令人困惑,因为我假设类的内部(在某些时候)将值解析为整数,但属性本身接受一个字符串......?那为什么会这样呢?

最佳答案

关于为什么这不起作用的一些附加信息。 Microsoft.AspNet.WebApi.Versioning包裹如下 semantic versioning要求主要部分和次要部分之间的分隔符是句号的规则。查看rules对于这个包。

通过一些技巧,可以获取 API 版本控制包来解析下划线。这是非常基本的代码,可能还没有准备好生产,但应该给你一个方向。您需要的第一件事是自定义路由约束(本质上是扯掉 the default one ):

public class CustomApiVersionRouteConstraint : IHttpRouteConstraint
{
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName,
IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
if (string.IsNullOrEmpty(parameterName))
{
return false;
}

var properties = request.ApiVersionProperties();
var versionString = "";

if (values.TryGetValue(parameterName, out object value))
{
//This is the real 'magic' here, just replacing the underscore with a period
versionString = ((string) value).Replace('_', '.');

properties.RawApiVersion = versionString;
}
else
{
return false;
}


if (ApiVersion.TryParse(versionString, out var requestedVersion))
{
properties.ApiVersion = requestedVersion;
return true;
}

return false;
}
}

并确保 Web API 使用新约束:

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

config.MapHttpAttributeRoutes(constraintResolver);

关于c# - 是否可以在 Controller 的 ApiVersion 属性中包含下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49904764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com