【问题标题】:How to get AutoRest to split out API's by controller如何让 AutoRest 通过控制器拆分 API
【发布时间】:2017-02-15 15:27:11
【问题描述】:

我现在正在使用:

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

生成我的ASP.NET CoreRest Client

令人烦恼的是AutoRestAPI 中的所有controllers 创建了一个file/class。我使用pre-ASP.NET Core auto-generators 将每个controller 拆分为自己的file/class,有没有办法在AutoRest 中强制这种行为?

【问题讨论】:

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


    【解决方案1】:

    根据 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 Method

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

    进入这种生成的service method

    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));
        }
    }
    

    【讨论】:

      【解决方案2】:

      这适用于 .net core 2.1,在我的控制器中我定义了一个 SwaggerOperationAttribute。

          public class SwashbuckleOperationFilter : IOperationFilter
      {
          public void Apply(Operation operation, OperationFilterContext context)
          {
              if (context.ApiDescription.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
              {
                  operation.OperationId = $"{controllerActionDescriptor.ControllerName}_{operation.OperationId}";
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-20
        • 1970-01-01
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多