无法通过 IActionResult 端点 return 匿名对象
Unable to return anonymous object through an IActionResult Endpoint
当我尝试从我的 ASP .NET Core 6 Web-Api 项目中的端点 return 匿名 object/DTO 时遇到问题。我得到一个错误,它不能隐式地将类型转换为 IActionResult。我使用的代码是在 this answer 中建议的,但不幸的是它对我不起作用,我也尝试使用将匿名对象映射到 dto,但这也不起作用。任何帮助将不胜感激,提前致谢。
我也尝试 return 一个 Json(output),就像答案中建议的那样,但是,Json() 我无法为 "Json" 指定一个 using,我也尝试过return 通过修改端点的 return 类型得到 JsonResult(output),但这也不起作用。
这是我使用的代码:
[HttpGet]
public IActionResult GetAllEndpoints()
{
var endpoints = _endpoinDataSources
.SelectMany(es => es.Endpoints)
.OfType<RouteEndpoint>();
var output = endpoints.Select(
e =>
{
var controller = e.Metadata
.OfType<ControllerActionDescriptor>()
.FirstOrDefault();
var action = controller != null
? $"{controller.ControllerName}.{controller.ActionName}"
: null;
var controllerMethod = controller != null
? $"{controller.ControllerTypeInfo.FullName}:{controller.MethodInfo.Name}"
: null;
return new GetEndpointsDto
{
Method = e.Metadata.OfType<HttpMethodMetadata>().FirstOrDefault()?.HttpMethods?[0],
Route = $"/{e.RoutePattern.RawText.TrimStart('/')}",
Action = action,
ControllerMethod = controllerMethod
};
}
);
return output;
}
我还通过控制器构造函数注入了 EndPointDataSource:
public MyController(IEnumerable<EndpointDataSource> endpointSources)
{
_endpoinDataSources = endpointSources;
}
你应该尝试 public ActionResult<object> GetAllEndpoints()
然后 return 类似 return Ok(output)
的东西或者也只是 return output
我意识到我正在 returning 输出我的 API 并且工作正常
您需要更具体 return 类型:
[HttpGet]
public IActionResult<List<GetEndpointsDto>> GetAllEndpoints()
{
...
return Ok(output.toList())
}
当我尝试从我的 ASP .NET Core 6 Web-Api 项目中的端点 return 匿名 object/DTO 时遇到问题。我得到一个错误,它不能隐式地将类型转换为 IActionResult。我使用的代码是在 this answer 中建议的,但不幸的是它对我不起作用,我也尝试使用将匿名对象映射到 dto,但这也不起作用。任何帮助将不胜感激,提前致谢。
我也尝试 return 一个 Json(output),就像答案中建议的那样,但是,Json() 我无法为 "Json" 指定一个 using,我也尝试过return 通过修改端点的 return 类型得到 JsonResult(output),但这也不起作用。
这是我使用的代码:
[HttpGet]
public IActionResult GetAllEndpoints()
{
var endpoints = _endpoinDataSources
.SelectMany(es => es.Endpoints)
.OfType<RouteEndpoint>();
var output = endpoints.Select(
e =>
{
var controller = e.Metadata
.OfType<ControllerActionDescriptor>()
.FirstOrDefault();
var action = controller != null
? $"{controller.ControllerName}.{controller.ActionName}"
: null;
var controllerMethod = controller != null
? $"{controller.ControllerTypeInfo.FullName}:{controller.MethodInfo.Name}"
: null;
return new GetEndpointsDto
{
Method = e.Metadata.OfType<HttpMethodMetadata>().FirstOrDefault()?.HttpMethods?[0],
Route = $"/{e.RoutePattern.RawText.TrimStart('/')}",
Action = action,
ControllerMethod = controllerMethod
};
}
);
return output;
}
我还通过控制器构造函数注入了 EndPointDataSource:
public MyController(IEnumerable<EndpointDataSource> endpointSources)
{
_endpoinDataSources = endpointSources;
}
你应该尝试 public ActionResult<object> GetAllEndpoints()
然后 return 类似 return Ok(output)
的东西或者也只是 return output
我意识到我正在 returning 输出我的 API 并且工作正常
您需要更具体 return 类型:
[HttpGet]
public IActionResult<List<GetEndpointsDto>> GetAllEndpoints()
{
...
return Ok(output.toList())
}