Problems trying to return a view using an API controller

I am implementing a REST web API. I use the examples from Adam Freeman Pro ASP.NET MVC5 as a starting point, but adapting it to the web API method.

Below is my code:

public class AdminController : ApiController { private IUserRepository _repository; public AdminController(IUserRepository repository) { _repository = repository; } public ActionResult Index() { return View(_repository.Users); } } 

The book AdminController implements Controller not ApiController , but if I do this, I get errors that there is no constructor without parameters. I need a constructor to accept parameters so that I can embed dependencies. Therefore, why I changed to ApiController , but now it does not recognize the View .

What do I need to use instead of View for ApiController ?

I found this question , but the answer was basically "you don't need to use ApiController here, just use Controller" so that they don't help me.

+6
source share
3 answers

You have two problems. Allow them separately.

1. Do I need to use ApiController or Controller ?:

Someone already answered this here: The difference between ApiController and the controller in ASP.NET MVC .

The first significant difference you will notice is that actions in the web API controllers do not return views, they return data.

ApiControllers specialize in returning data. For example, they take care of transparently serializing data in the format requested by the client.

So, if you want to return the View , you need to use a simple ol ' Controller . WebApi "path" is similar to a web service in which you exchange data with another service (returning JSON or XML to this service, not to the view ). Therefore, whenever you want to return a web page ( View ) to a user, you are not using the web API.

In other words, the web API is returning data to another service (to return JSON or XML), and not to the user.

2. But if I use the controller, I get "no parameters" errors.

Ok, now we have your real problem. Do not try to reinvent the wheel and fight ASP.NET to inject dependencies! A tool already exists for resolving dependency injection and sorting the "no parameters" error: Ninject .

If you are already using Ninject and still getting this error, you are doing something wrong with Ninject. Try to repeat the installation and configuration steps and see some tips or questions about a non-parameter error with Ninject to use

+11
source

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:

+2
source

Use Controller to display your regular views. The ApiController action returns only data that is serialized and sent to the client. But still you want to visualize the view from the APIcontroller, then there may be another way, click the link below for reference:

https://aspguy.wordpress.com/2013/09/10/web-api-and-returning-a-razor-view/

0
source

Source: https://habr.com/ru/post/978685/


All Articles