Problem
Using ApiControllerAttribute and RouteAttribute on controllers and actions, everythings work fine.
When I change the code to work with Convetional Routing, the Identity property in request is always set to null.
Code with ApiControllerAttribute (Identity loaded in request)
[ApiController]
[Route("api/[controller]")]
Public Class Main : ControllerBase
{
[HttpPost(nameof(GetExternalRemoteExternal))]
public async Task<GetByIdentityResponse<RemoteExternal>> GetExternalRemoteExternal(GetByIdentityRequest<RemoteExternalIdentity> request)
{
return await GetExternal<RemoteExternal, RemoteExternalIdentity>(request);
}
}
startup.cs
app.UseEndpoints(endpoints => endpoints.MapControllers());
Code with Convetional Routing (request has null Identity)
Public Class Main : ControllerBase
{
[HttpPost]
public async Task<GetByIdentityResponse<RemoteExternal>> GetExternalRemoteExternal(GetByIdentityRequest<RemoteExternalIdentity> request)
{
return await GetExternal<RemoteExternal, RemoteExternalIdentity>(request);
}
}
startup.cs
app.UseEndpoints(endpoints => endpoints.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action}")) //Not work even with "api/{controller}/{action}/{?id}"
Common code
public class GetByIdentityRequest<TIDentity> : ServiceRequest
where TIDentity : BaseIdentity
{
public TIDentity Identity { get; set; }
}
public class RemoteExternalIdentity : BaseIdentity
{
public int IdX { get; set; }
}
JSON
{"$id":"1","Identity":{"$id":"2","IdX":10000}}
API LINK
.../api/Main/GetExternalRemoteExternal
[FromBody]
before the parameter typeGetByIdentityRequest<RemoteExternalIdentity>
. The[ApiController]
attribute adds a few conventions which may cause the difference here. – Mcbroom