ASP.NET核心控制器继承和路由

发布于 2025-01-27 13:24:28 字数 1261 浏览 2 评论 0原文

在两个解决方案中,我的控制器类别分布在几个项目上。在解决方案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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2025-02-03 13:24:28

显然[路由]属性存在于两个命名空间中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文