17

I am developing a web API for my website and have ran into a problem.

At the moment, the API is supposed to return details from the specified user.

This is my controller for accounts:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using System.Net;
using RoditRepoAPIV2.Models;

namespace RoditRepoAPIV2.Controllers
{
    public class AccountController : Controller
    {
        Account[] products = new Account[] 
        { 
            //accounts will be added...
        };

        public IEnumerable<Account> GetAllAccounts()
        {
            return products;
        }

        public IHttpActionResult GetAccount(int id)
        {
            var account = products.FirstOrDefault((p) => p.Id == id);
            if (account == null)
            {
                return NotFound();
            }
            return Ok(account);
        }

    }
}

Although most of this code is copied from the tutorial here, Visual Studio complains that 'NotFound()' and 'Ok(account)' do not exist in the current context. I have updated all of the NuGet packages to version 5.1.2+ and I still get this error.

I have done some research and found that it seems to work for other people...

I would much appreciate it if anyone could respond with a working solution! Thank you.

0

3 Answers 3

36

You need to inherit your controller from ApiConroller - that is where these methods are defined:

public class AccountController : ApiController
Sign up to request clarification or add additional context in comments.

Comments

2

from aspnet core 6

Replace

return NotFound() and HttpNotFound();

with

return new StatusCodeResult(404);

Comments

0

You need to inherit your controller from ControllerBase, which means that your controller class should be:

ClassController : ControllerBase,

where Class, your controller name.

1 Comment

At least for Asp.Net Core 3.1, 5.0 and after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.