tRuEsAtM
tRuEsAtM

Reputation: 3678

Getting 404 when trying to hit the WebAPI controller method

I have created a project using ASP.NET Core Web Application (.NET Framework). My project structure:

Project Structure

My values controller is inherited from an MVC controller. When I hit http://localhost:20798/api/values, I do get the desired response. Values Controller

I added ConsumerScore controller, this time a WebAPI controller inheriting from ApiController. But now when I hit http://localhost:20798/api/consumerscore, I am getting 404 response. Error Response

My ConsumerScore controller looks like: ConsumerScore controller

How to resolve this error?

Upvotes: 2

Views: 2270

Answers (2)

Paulo Kinjo
Paulo Kinjo

Reputation: 34

The error is with the "ApiController" you should use just Controller in your implementation.

There is indeed to particular ApiController class anymore since MVC and WebAPI have been merged in ASP.NET Core. However, the Controller class of MVC brings in a bunch of features you probably won't need when developing just a Web API, such as a views and model binding.

for more see: Is ApiController deprecated in .NET CORE?

Upvotes: 1

Amir
Amir

Reputation: 242

Seems like you are confusing the idea of "MVC" and "Web API".

ASP.net MVC is used for serving web pages while the Web API is supposed to be serving data in a negotiable way (json/xml/...) to http requests.

Adding an "api/*" to an MVC route and serving data from an MVC Controller is not the best idea and will not turn it into a useful web service.

From the look of your methods, I suppose what you need is an ApiController but you cannot create the http request by copying the "~/api/*" to your web browser and you need to use tools like fiddler or your browser's debugger.

Upvotes: 0

Related Questions