首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让AutoRest按控制器拆分接口

如何让AutoRest按控制器拆分接口
EN

Stack Overflow用户
提问于 2016-10-07 02:47:01
回答 2查看 1.2K关注 0票数 3

现在我使用的是:

代码语言:javascript
复制
AutoRest\AutoRest.exe -Input %jsonUrl% -Namespace %projectName%ClientAutoGen -OutputDirectory %projectName%Client

来生成我的ASP.NET Core Rest Client

令人恼火的是,AutoRest为该file/class中的所有controllers创建一个API。我使用过pre-ASP.NET Core auto-generators,它会将每个controller拆分成各自的file/class,有没有办法在AutoRest中强制执行这种行为

EN

回答 2

Stack Overflow用户

发布于 2016-10-07 09:11:37

根据AutoRest团队在GitHub (此处:https://github.com/Azure/autorest/issues/1497 )上提供的有用答案,答案是在您希望发生拆分的OperationId中使用_。这是我的Filter版本

代码语言:javascript
复制
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中像这样使用

代码语言:javascript
复制
services.AddSwaggerGen(options =>
{
    options.OperationFilter<SwashbuckleOperationFilter>();
    //...
}

要像这样转换API Controller Method

代码语言:javascript
复制
[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

代码语言:javascript
复制
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));
    }
}
票数 3
EN

Stack Overflow用户

发布于 2018-05-25 21:52:21

这将与.net核心2.1一起工作,在我的控制器中,我定义了一个SwaggerOperationAttribute。

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

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39903534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档