【发布时间】:2020-06-02 09:16:13
【问题描述】:
问题
在控制器和动作上使用 ApiControllerAttribute 和 RouteAttribute,一切正常。
当我更改代码以使用传统路由时,请求中的 Identity 属性始终设置为 null。
带有 ApiControllerAttribute 的代码(在请求中加载身份)
[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());
带有传统路由的代码(请求具有空标识)
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}"
常用代码
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 链接
.../api/Main/GetExternalRemoteExternal
【问题讨论】:
-
你能显示你的控制器的注释吗?另外,您使用什么 URL 来发出请求?
-
尝试在参数类型
GetByIdentityRequest<RemoteExternalIdentity>之前放置一个[FromBody]。[ApiController]属性添加了一些约定,这可能会导致此处的差异。
标签: json asp.net-core .net-core routes asp.net-apicontroller