ASP.NET核心控制器继承和路由
在两个解决方案中,我的控制器类别分布在几个项目上。在解决方案A中,路由可以按预期工作,在解决方案b中无效。我不知道为什么?
[ApiController]
[Route("/api/[controller]")]
public abstract class ApiControllerBase : ControllerBase, IController
{
...
}
public class ApiCrudControllerBase<T> : ApiControllerBase where T : IModel, new()
{
...
}
[Route("/portal-api/[controller]")]
public class PortalApiCrudController<T> : ApiCrudControllerBase<T> where T : IModel, new()
{
...
}
public class RoleController : PortalApiCrudController<Role>
{
}
[HttpPost]
[Route("{roleId}/userGroup/{userGroupId}")]
public async Task<ActionResult<ApiListResponse<UserGroup>>> AddUserGroup2Role(string
roleId, string userGroupId)
{
...
}
[HttpGet]
[Route("/portal-api/roles")]
public ActionResult<ApiListResponse<Role>> Read([FromQuery] FilterSettings filterSettings)
{
...
}
}
Swagger为端点提供了跟随路径。
应用程序A:
POST /portal-api/role/{roleId}/userGroup/{userGroupId}
GET /portal-api/roles
应用程序B:
POST /api/role/{roleId}/userGroup/{userGroupId}
GET /portal-api/roles
似乎在应用程序B中,路由中的继承不起作用。我已经检查了Nuget软件包版本,启动配置,....到目前为止,我找不到任何区别。
有什么建议吗?
In 2 solutions I have the same controller classes spread over several Projects. In solution A the routing works as expected, in solutionB not. I have no clue why ?
[ApiController]
[Route("/api/[controller]")]
public abstract class ApiControllerBase : ControllerBase, IController
{
...
}
public class ApiCrudControllerBase<T> : ApiControllerBase where T : IModel, new()
{
...
}
[Route("/portal-api/[controller]")]
public class PortalApiCrudController<T> : ApiCrudControllerBase<T> where T : IModel, new()
{
...
}
public class RoleController : PortalApiCrudController<Role>
{
}
[HttpPost]
[Route("{roleId}/userGroup/{userGroupId}")]
public async Task<ActionResult<ApiListResponse<UserGroup>>> AddUserGroup2Role(string
roleId, string userGroupId)
{
...
}
[HttpGet]
[Route("/portal-api/roles")]
public ActionResult<ApiListResponse<Role>> Read([FromQuery] FilterSettings filterSettings)
{
...
}
}
Swagger gives the followins paths to the endpoints.
Application A:
POST /portal-api/role/{roleId}/userGroup/{userGroupId}
GET /portal-api/roles
Application B:
POST /api/role/{roleId}/userGroup/{userGroupId}
GET /portal-api/roles
It seems that in Application B the inheritance in routing is not working. I've checked nuget package versions, startup configuration, .... I can't find any difference so far.
Any suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然[路由]属性存在于两个命名空间中。
Microsoft.aspnetcore.mvc
microsoft.aspnetcore.com.ponents portents``
跨地点''中的一个与其他路线相比,还有一个不同的命名空间。那里向南。
大概在重构和“在解决方案中添加丢失的使用”时,其中一个弄错了。
Apparently [Route] attribute exist in 2 namespaces.
Microsoft.AspNetCore.Mvc
Microsoft.AspNetCore.Components
One of the 'intermidiate' got a different namespace for route then the others. There it went south.
Probably when refactoring and 'add missing usings in solution' one of them got the wrong one.