I define modules for my app, and I add resources as application parts to the application.
However, I'd like to map all endpoints from each module to its own subpath.
How can I achieve this?
I define modules for my app, and I add resources as application parts to the application.
However, I'd like to map all endpoints from each module to its own subpath.
How can I achieve this?
On
You can simply separate base controllers and customize their routes. With this you can limit authorizations and produce consume types as well.
Base Controller
[ApiController]
[AllowAnonymous]
[Consumes(ApiContentTypes.ApplicationJson)]
[Produces(ApiContentTypes.ApplicationJson)]
public class BaseApiController : ControllerBase
{
}
Employee Module Base Controller
[Route("api/EmployeeModule/[Controller]/[Action]")]
[Authorize(Roles = Roles.EmployeeAdmin)]
public class EmployeeModuleController : BaseApiController
{
}
Employee Module Sub-Controllers
public class ManagerEmployeeController : EmployeeModuleController
{
}
Another module base controller
[Route("api/Story")]
public class StoryModuleController : BaseApiController
{
}
Sub-base controller
[Route("Instagram/[Action]")]
[Authorize(Policy = Policies.InstagramPolicy)]
public class InstagramStoryController : StoryModuleController
{
public async Task<IResponse> GetAsync(CancellationToken ct = default)
{
await Task.Yield();
return new SuccessResponse("Successfully Retrieved");
}
}
We can implement this requirementby using
IApplicationModelConvention. You can check the test result first and tell me is it you want(I am using the official repo).If this is what you want to achieve, please follow my steps.
Step 1. Download the repo and create ModuleRoutingConvention.cs file.
Step 2. Modify the EntityTypes.cs file, like below.
Step 3. Register it in your Startup.cs
Step 4. Add attributes on the controllers in your Class Library Project.