Aspnetcore.docs: ActionResult Errors

Created on 1 May 2018  Â·  2Comments  Â·  Source: dotnet/AspNetCore.Docs

Adding the following methods to TodoController:

[HttpGet]
public ActionResult> GetAll()
{
return _context.TodoItems.ToList();
}

[HttpGet("{id}", Name = "GetTodo")]
public ActionResult GetById(long id)
{
var item = _context.TodoItems.Find(id);
if (item == null)
{
return NotFound();
}
return item;
}

generated the following error:
"The non-generic type 'ActionResult' cannot be used with type arguments"

My fix is as follows:

    [HttpGet]
    public ActionResult GetAll()
    {
        return new ObjectResult(_context.TodoItems.ToList());
    }

    [HttpGet("{id}", Name = "GetTodo")]
    public ActionResult GetById(long id)
    {
        var item = _context.TodoItems.Find(id);
        if (item == null)
        {
            return NotFound();
        }
        return new ObjectResult(item);
    }

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms

Most helpful comment

Thanks Scott,

That fixed it. I'll now know better in the future. Much appreciated

All 2 comments

@Gordon-Fredericks The [ApiController] attribute and the ActionResult<T> type assume you're using ASP.NET Core 2.1. That's the release in which this feature was introduced. You must have selected "ASP.NET Core 2.1 Preview 2" from the version selector at the top of the page. Select 2.0 instead to view that version of the tutorial.

I see now where the confusion stems from in this 2.1 tutorial version. The screenshot at the top of the tutorial reflects ASP.NET Core 2.0. I'll address this.

Thanks Scott,

That fixed it. I'll now know better in the future. Much appreciated

Was this page helpful?
0 / 5 - 0 ratings