2

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 ?

BrilBroeder
  • 1,409
  • 3
  • 18
  • 37
  • What do you mean for not working? As you said you use swagger to generate the url, so that is to say your request url should be correct. What's the error message? And did your two project have references? For example, Application B adds a reference with application A, if you send request to `/portal-api/roles`, you will get error `AmbiguousMatchException: The request matched multiple endpoints` because your application A also contains the same endpoint. – Rena May 10 '22 at 01:48
  • The 2 applications are totally independent off each other. Yes I do get exceptions that paths in application B are not unique. Investigating this I discovered the paths are not created correctly, hence this post. – BrilBroeder May 10 '22 at 06:08
  • Hi @BrilBroeder, So do you mean you think the Get method generates the wrong url? If so, what's the correct url you think it should generate? Are both of your two applications use the same `RoleController` with inheritance? – Rena May 10 '22 at 06:14
  • They should generate the same path. – BrilBroeder May 10 '22 at 06:54
  • Hi @BrilBroeder, `the same path`? So your point maybe the POST method does not generate the same path? If so, what is the version of your application B and what's the structure, mvc or web api or? – Rena May 10 '22 at 08:56
  • GET : the route is set directly on the method. POST: the route is generated through inheritance, in one app this works, in another is does not – BrilBroeder May 10 '22 at 09:49

1 Answers1

0

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.

BrilBroeder
  • 1,409
  • 3
  • 18
  • 37