【发布时间】:2019-07-28 09:59:18
【问题描述】:
我正在尝试从服务内部(控制器外部)创建到 API 端点的链接。
这是控制器及其基类。我在 ASP.NET Core 中使用 API 版本控制和区域。
[ApiController]
[Area("api")]
[Route("[area]/[controller]")]
public abstract class APIControllerBase : ControllerBase
{
}
[ApiVersion("1.0")]
public class WidgetsController : APIControllerBase
{
[HttpGet("{id}"]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Widget>> Get(Guid id)
{
// Action...
}
}
API 版本配置:
services.AddApiVersioning(options =>
{
options.ApiVersionReader = ApiVersionReader.Combine(
new QueryStringApiVersionReader
{
ParameterNames = { "api-version", "apiVersion" }
},
new HeaderApiVersionReader
{
HeaderNames = { "api-version", "apiVersion" }
});
});
我实际尝试使用 LinkGenerator 的地方:
_linkGenerator.GetPathByAction(
_accessor.HttpContext,
action: "Get",
controller: "Widgets",
values: new
{
id = widget.Id,
apiVersion = "1.0"
}
)
我已经尝试了 LinkGenerator 的各种变体。我已经使用了 HttpContext 重载,我已经使用了没有它的重载,我已经包含了 apiVersion 参数并省略了它,我已经从控制器中完全删除了[ApiVersion]。一切都会回来null。如果我路由到像 GetPathByAction("Index", "Home") 这样的普通 MVC 控制器,我会得到一个我应该得到的 URL,所以我认为它一定与我的 API 区域或版本设置有关。
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc url-routing