An API controller
is a controller
that provides a RESTful response. You cannot return a view from it. Instead, consider returning a response (s) that causes the client to request an action to redirect to another controller (if you need to pass arguments) to return the view.
In your case, it doesn't seem like you need an API; in this case, just try this (change what you inherit):
public class AdminController : Controller { private IUserRepository _repository; public AdminController(IUserRepository repository) { _repository = repository; } public ActionResult Index() { return View(_repository.Users); } }
I will try to explain what the API should do anyway. The web API should only return information. An HTTP response about what the action should do.
For example, to create a new client, the API must have a method (decorated with POST) to obtain information from the client application (it can be anything: web, windows, mobile, windows service, etc.). This information should be processed by the API (or other layers in a possible architecture) and return an HTTP status code, for example 200 - OK
if it was good or 400 - Bad Request
if an error occurred. Therefore, when I said that you should consider returning information, you can simply return the DTO object to provide the result.
Both types of projects use the principles of MVC, but they are used in a different context. Take a look at these articles:
Also check out the ASP.NET website on how they work:
source share