1

Update

Tried to clarify my problem


I have a ASP.NET 5 Web Api application. I'm trying to create a controller class that makes use of a custom base controller. As soon as I add a constructor to the base controller then MVC can no longer find the Get() end point defined in the Generic class.

This works:

When I navigate to /api/person then the Get() end point is triggered defined in the Generic class

Generic:

public class Generic
{
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

PersonController

[Route("api/[controller]")]
public class PersonController : Generic
{
}

This doesn't work When I navigate to /api/person then the Get() end point is not triggered. The only thing that is added is a constructor in both the Generic class and the PersonController class.

public class Generic
{
    protected DbContext Context { get; set; }

    public Generic(DbContext context)
    {
        Context = context;
    }

    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

PersonController

[Route("api/[controller]")]
public class PersonController : Generic
{
    public PersonController(DbContext context) : base(context) { }
}

Is this a bug or am I doing something wrong?

5
  • 2
    What do you mean by "This doesn't work" and "can no longer find"? Commented Dec 24, 2015 at 11:36
  • Do you use Dependency Injection ? If you are, can you show registers ? Commented Dec 24, 2015 at 11:38
  • 8
    Your CustomBase must inherit from Controller... And if you have that, you should read How to Ask and create a minimal reproducible example. Show the exact error you get and the research you did to resolve that error. If you add a parameterless constructor, then the default controller factory indeed cannot instantiate your controllers anymore, and you can solve that using dependency injection or by introducing your own controller factory. All of this has been covered before, so try searching. Commented Dec 24, 2015 at 11:39
  • @CodeCaster That doesn't work. Also, it's ASP.NET 5. You're not required to add the base Controller class. But even when I do add it, I still cannot call my Get() endpoint. Agian, it does work when I remove the constructors from PersonController and my Generic class Commented Dec 24, 2015 at 13:42
  • You're right, I missed that. Anyway the rest of my comment still stands. You must instruct MVC to inject your context into your controller constructor. Commented Dec 24, 2015 at 13:47

1 Answer 1

2

Your problem has nothing to do with inheriting from another base class. I think the problem is, a proper implementation of your DbContext is not being injected to the constructor. Since asp.net 5 is so modular/dependency injectable, you need to explicitly configure it.

In the ConfigureServices method in your Startup.cs class, register your DbContext service so MVC will use that and inject that to your class controller when needed.

public void ConfigureServices(IServiceCollection services)
{
    services.AddEntityFramework()
        .AddDbContext<DbContext>();

    services.AddMvc();
}
Sign up to request clarification or add additional context in comments.

Comments

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.