我正在使用ASP.NET Core 2.0.0 Web API尝试构建一个控制器来进行数据库插入。信息可以成功地插入到数据库中,但是返回CreatedAtRoute抛出了一个“InvalidOperationException: No route matches the supplied values.”的错误。到目前为止,我在网上找到的所有内容都说这是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);
}
有人能给我解释一下吗?