c# - 如何让 AutoRest 通过 Controller 拆分 API

标签 c# asp.net-core rest-client auto-generate autorest

现在我正在使用:

AutoRest\AutoRest.exe -Input %jsonUrl% -Namespace %projectName%ClientAutoGen -OutputDirectory %projectName%Client

生成我的 ASP.NET Core Rest Client

令人烦恼的是 AutoRest 为该 API 中的所有 controllers 创建了一个文件/类 .我使用了 pre-ASP.NET Core auto-generators 将每个 controller 拆分成它们自己的 file/class,有没有办法在 AutoRest 中强制执行此行为?

最佳答案

根据 AutoRest 团队在 GitHub 上的有用回答(​​此处:https://github.com/Azure/autorest/issues/1497),答案是在要进行拆分的 OperationId 中使用 _。这是我的 Filter 版本:

public class SwashbuckleOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        try
        {
            var pathSegments = context.ApiDescription.RelativePath.Split(new[] { '/' }).ToList();

            var version = string.Empty;
            var controller = string.Empty;
            if (pathSegments.Count > 1)
            {
                version = pathSegments[0];
                controller = pathSegments[1] + "_";

                pathSegments = pathSegments.Skip(2).Where(x => !x.Contains("{")).ToList();
            }

            string httpMethod = FirstCharToUpper(context.ApiDescription.HttpMethod);
            var routeName = context.ApiDescription.ActionDescriptor?.AttributeRouteInfo?.Name ?? string.Empty;

            operation.OperationId = $"{version}{controller}{httpMethod}{string.Join("", pathSegments)}{routeName}";
        }
        catch (Exception ex)
        {
            throw new Exception("Are you missing the [Route(\"v1/[controller]/\")] on your Controller?", ex);
        }
    }

    private string FirstCharToUpper(string input)
    {
        if (String.IsNullOrEmpty(input))
            return string.Empty;

        input = input.Trim().ToLower();

        return input.First().ToString().ToUpper() + new string(input.Skip(1).ToArray());
    }
}

StartUp 中这样使用:

services.AddSwaggerGen(options =>
{
    options.OperationFilter<SwashbuckleOperationFilter>();
    //...
}

像这样转换一个API Controller 方法:

[Route("v1/[controller]/")]
public class ThingController : Controller
{
    [HttpGet("ById/{id}")]
    [Produces(typeof(ThingDTO))]
    public async Task<IActionResult> GetThing([FromRoute] long id)
    {
        // Your implementation
    }
}

进入这种生成的服务方法:

public class ThingClient : IThingClient
{
    private readonly AppSettings appSettings;
    private readonly IMapper mapper;
    private IV1Thing api;

    public ThingClient(IOptions<AppSettings> appSettingsOptions, IMapper mapper)
    {
        appSettings = appSettingsOptions.Value;
        this.mapper = mapper;
    }

    private IV1Thing service => api ??
                (api = new V1Thing(new ThingService(new Uri(appSettings.URLs.ThingService))));

    public async Task<IThing> GetByIdAsync(long thingId)
    {
        return mapper.Map<IThing>(await service.GetByIdAsync(thingId));
    }
}

关于c# - 如何让 AutoRest 通过 Controller 拆分 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39903534/

相关文章:

java - 如何在UriBuilder.fromUri中指定RESTful请求参数?

java - 使用 Elastic Search 5.5.0 时如何正确关闭 Raw RestClient 以获得最佳性能?

java - 从异步 rest 模板 spring 返回值

c# - MSBuild BuildInParallel,无法运行的自定义任务生成过程

c# - 从远程服务器自动更新 asp.net web 应用程序

c# - 在 asp.net 中中继请求(转发请求)

c# - Entity Framework 核心3.0-捕获错误

c# - 如何解析带有过滤参数的集合?

c# - 单步执行引用解决方案中的代码

c# - net core 2.2 到 3.0 身份验证迁移 AddOpenIdConnect