如何自定义 abp CrudAppService / AsyncCrudAppService 上的路由?

huangapple go评论75阅读模式
英文:

How do you customize the Route on abp CrudAppService / AsyncCrudAppService

问题

I am experimenting with the abp CrudAppService (https://docs.abp.io/en/abp/latest/Application-Services#crud-application-services), which allows me to set up CRUD endpoints with minimal code.

As an example, I can create the following base class:

    public class BaseAsyncCrudAppService<TEntity, TDto> : AsyncCrudAppService<TEntity, TDto, Guid, PagedAndSortedResultRequestDto,
        TDto, TDto> where TEntity : class, IEntity<Guid> where TDto : IEntityDto<Guid>
    {
        public BaseAsyncCrudAppService(IRepository<TEntity, Guid> repository)
            : base(repository)
        {

        }
    }

And then create enpoint sets like these:

    public class MyFooAppService : BaseAsyncCrudAppService<MyFooEntity, MyFooDto>,
        IMyFooAppService
    {

        public MyFooAppService(IRepository<MyFooEntity, Guid> repository) : base(repository)
        {
        }
    }

And

    public class MyBarAppService : BaseAsyncCrudAppService<MyBarEntity, MyBarDto>,
        IMyBarAppService
    {

        public MyBarAppService(IRepository<MyBarEntity, Guid> repository) : base(repository)
        {
        }
    }

This will produce the following endpoints:

    GET /api/services/app/MyFoo/Get
    GET /api/services/app/MyFoo/GetAll
    POST /api/services/app/MyFoo/Create
    PUT /api/services/app/MyFoo/Update
    DELETE /api/services/app/MyFoo/Delete


    GET /api/services/app/MyBar/Get
    GET /api/services/app/MyBar/GetAll
    POST /api/services/app/MyBar/Create
    PUT /api/services/app/MyBar/Update
    DELETE /api/services/app/MyBar/Delete

What I would like to know is how I could alter the path where these endpoints get served. I don't need to do it individually for each method, but I would like to set it for example

    GET /api/services/app/baz/MyFoo/Get
    GET /api/services/app/baz/MyFoo/GetAll
    POST /api/services/app/baz/MyFoo/Create
    PUT /api/services/app/baz/MyFoo/Update
    DELETE /api/services/baz/app/MyFoo/Delete

If I were to code the endpoints manually, I would do it like this:

    [Route("api/services/app/baz/[controller]/Get")]
    public async Task GetMyFoo(Guid id)
    {
        ...
    }

Is there a way to do this on class level on the MyFooAppService class?

英文:

I am experimenting with the abp CrudAppService (https://docs.abp.io/en/abp/latest/Application-Services#crud-application-services), which allows me to set up CRUD endpoints with minimal code.

As an example, I can create the following base class:

public class BaseAsyncCrudAppService<TEntity, TDto> : AsyncCrudAppService<TEntity, TDto, Guid, PagedAndSortedResultRequestDto,
    TDto, TDto> where TEntity : class, IEntity<Guid> where TDto : IEntityDto<Guid>
{
    public BaseAsyncCrudAppService(IRepository<TEntity, Guid> repository)
        : base(repository)
    {

    }
}

And then create enpoint sets like these:

public class MyFooAppService : BaseAsyncCrudAppService<MyFooEntity, MyFooDto>,
    IMyFooAppService
{
    
    public MyFooAppService(IRepository<MyFooEntity, Guid> repository) : base(repository)
    {
    }
}

And

public class MyBarAppService : BaseAsyncCrudAppService<MyBarEntity, MyBarDto>,
    IMyBarAppService
{
    
    public MyBarAppService(IRepository<MyBarEntity, Guid> repository) : base(repository)
    {
    }
}

This will produce the following endpoints:

GET /api/services/app/MyFoo/Get
GET /api/services/app/MyFoo/GetAll
POST /api/services/app/MyFoo/Create
PUT /api/services/app/MyFoo/Update
DELETE /api/services/app/MyFoo/Delete


GET /api/services/app/MyBar/Get
GET /api/services/app/MyBar/GetAll
POST /api/services/app/MyBar/Create
PUT /api/services/app/MyBar/Update
DELETE /api/services/app/MyBar/Delete

What I would like to know is how I could alter the path where these endpoints get served. I don't need to do it individually for each method, but I would like to set it for example

GET /api/services/app/baz/MyFoo/Get
GET /api/services/app/baz/MyFoo/GetAll
POST /api/services/app/baz/MyFoo/Create
PUT /api/services/app/baz/MyFoo/Update
DELETE /api/services/baz/app/MyFoo/Delete

If were to code the endpoints manually, I would do it like this:

[Route("api/services/app/baz/[controller]/Get")]
public async Task GetMyFoo(Guid id)
{
    ...
}

Is there a way to do this on class level on the MyFooAppService class?

答案1

得分: 1

使用Microsoft.AspNetCore.Mvc.Route属性装饰基类,例如 [Route("/api/services/app/baz/[controller]")]

[Route("/api/services/app/baz/[controller]")]
public class BaseAsyncCrudAppService<TEntity, TDto> : AsyncCrudAppService<TEntity, TDto, Guid, PagedAndSortedResultRequestDto,
    TDto, TDto> where TEntity : class, IEntity<Guid> where TDto : IEntityDto<Guid>
{
    public BaseAsyncCrudAppService(IRepository<TEntity, Guid> repository)
        : base(repository)
    {

    }
}

要自定义各个端点的路由,可以重写方法并使用[HTTP动词属性]进行装饰1,例如,[HttpGet("/new/root/here")]

[HttpGet("/new/root/here")]
public override Task<TDto> GetAsync(EntityDto<Guid> id)
{
   return base.GetAsync(id);
}

将产生端点

GET  /new/root/here/

要扩展类级别指定的路由,请省略前导'/ '。

例如,[HttpGet("sub/root/here/")] 将使端点可用于

GET  /api/services/app/baz/[controller]/sub/root/here/
英文:

Decorate the base class with Microsoft.AspNetCore.Mvc.Route attribute, e.g. [Route(&quot;/api/services/app/baz/[controller]&quot;)].

[Route(&quot;/api/services/app/baz/[controller]&quot;)]
public class BaseAsyncCrudAppService&lt;TEntity, TDto&gt; : AsyncCrudAppService&lt;TEntity, TDto, Guid, PagedAndSortedResultRequestDto,
    TDto, TDto&gt; where TEntity : class, IEntity&lt;Guid&gt; where TDto : IEntityDto&lt;Guid&gt;
{
    public BaseAsyncCrudAppService(IRepository&lt;TEntity, Guid&gt; repository)
        : base(repository)
    {

    }
}

To customize the routes of the individual endpoints, you can override the methods and decorate them with HTTP verb attributes,

For example, [HttpGet(&quot;/new/root/here&quot;)]

[HttpGet(&quot;/new/root/here&quot;)]
public override Task&lt;TDto&gt; GetAsync(EntityDto&lt;Guid&gt; id)
{
   return base.GetAsync(id);
}

would produce endpoint

GET  /new/root/here/

To extend the route specified on class level, omit the leading '/'.

For example
[HttpGet(&quot;sub/root/here/&quot;)] would make the endpoint available at

GET  /api/services/app/baz/[controller]/sub/root/here/

huangapple
  • 本文由 发表于 2023年6月29日 20:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定