Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems trying to return the view with an API controller

I'm implementing a REST Web API. I'm using the examples from Adam Freeman's Pro ASP.NET MVC5 as a starting point but adapting it into the Web API way of doing it.

The below is my code:

public class AdminController : ApiController
{        
    private IUserRepository _repository;
    public AdminController(IUserRepository repository) 
    {
        _repository = repository; 
    }

    public ActionResult Index()
    {
        return View(_repository.Users);
    }
}

In the book, AdminController implemented Controller not ApiController, but if I do that then I get errors about there being no parameterless constructor. I need the constructor to take parameters so that I can inject the dependencies. So that's why I changed to ApiController but now it won't recognise View.

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

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

like image 886
starsplusplus Avatar asked Nov 25 '14 15:11

starsplusplus


People also ask

Can we return view from API controller?

You don't return a View from an API controller. But you can return API data from an MVC controller. The solution would be to correct the errors, not try to hack the API controller.

How do I return specific view on a controller?

To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required. return View("~/Views/Account/Register.


1 Answers

You are having two different problems. Let's solve them separately.

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

Someone already answered this here: Difference between ApiController and Controller in ASP.NET MVC.

The first major difference you will notice is that actions on Web API controllers do not return views, they return data.

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

So, if you want to return a View you need to use the simple ol' Controller. The WebApi "way" is like a webservice where you exchange data with another service (returning JSON or XML to that service, not a View). So whenever you want to return a webpage (View) for a user you don't use the Web API.

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

2. But if I use Controller then I get "parameterless constructor" errors.

Okay, now we've got to your real problem. Don't try to reinvent the wheel and fight with ASP.NET about doing dependency injection! A tool already exists to resolve dependency injection and sort out the "parameterless constructor" error: Ninject.

If you're already using Ninject and still getting that error, you're doing something wrong with Ninject. Try to repeat the installation and configuration steps, and see some tutorials or questions about parameterless error with Ninject use

like image 68
Wagner Leonardi Avatar answered Oct 22 '22 13:10

Wagner Leonardi