ASP.NET Core CreatedAtRoute没有路由匹配提供的值

Mik*_*ike 11 c# .net-core asp.net-core asp.net-core-webapi asp.net-core-mvc-2.0

使用ASP.NET Core 2.0.0 Web API,我正在尝试构建一个控制器来执行数据库插入.可以很好地将信息插入数据库,但返回CreatedAtRoute会抛出'InvalidOperationException:No route匹配提供的值.' 到目前为止,我在网上找到的所有内容都说这是早期预发布版本的ASP.NET Core的一个错误,并且已经修复,但我不确定该怎么做.以下是我的控制器代码:

[Produces("application/json")]
[Route("api/page")]
public class PageController : Controller
{
    private IPageDataAccess _pageData; // data access layer

    public PageController(IPageDataAccess pageData)
    {
        _pageData = pageData;
    }

    [HttpGet("{id}", Name = "GetPage")]
    public async Task<IActionResult> Get(int id)
    {
        var result = await _pageData.GetPage(id); // data access call

        if (result == null)
        {
            return NotFound();
        }

        return Ok(result);
    }

    [HttpPost]
    public async Task<IActionResult> Create([FromBody] Page page)
    {
        if (page == null)
        {
            return BadRequest();
        }

        await _pageData.CreatePage(page); // data access call

        // Return HTTP 201 response and add a Location header to response
        // TODO - fix this, currently throws exception 'InvalidOperationException: No route matches the supplied values.'
        return CreatedAtRoute("GetPage", new { PageId = page.PageId }, page);
    }
Run Code Online (Sandbox Code Playgroud)

有谁可能为我揭示这一点?

Nko*_*osi 16

参数需要匹配预期动作的路线值.

在这种情况下,你需要idPageId

return CreatedAtRoute(
    actionName: "GetPage", 
    routeValues: new { id = page.PageId },
    value: page);
Run Code Online (Sandbox Code Playgroud)