okay
okay

Reputation: 173

How do I implement two controllers in MVC

I am trying to implement ApiController in my StudentController, but the thing is that I have to implement BaseController also.

When I do this, it doesn't work

public class StudentController : BaseController, ApiController

How can I do this?

Upvotes: 2

Views: 3546

Answers (3)

Aran Mulholland
Aran Mulholland

Reputation: 23935

You can't. Multiple inheritance is not allowed in C#. the other answers give you possible workarounds but you can't derive from multiple base classes. C# only allows for a single inheritance chain.

Upvotes: 1

PSL
PSL

Reputation: 123739

Make your BaseController derived from ApiController.

public class BaseController: ApiController
{
...

public class StudentController : BaseController
{
...

If you want to use WebApi and MVC in the same application probably you can go with different namespaces.

namespace Applicationrootns.Controllers
{
    public class StudentController : Controller
    {
        ....
    }
}

namespace Applicationrootns.Controllers.Api
{
    public class StudentController : ApiController
    {
       ....
    }
}

Now with this you can access

applicaitonbase/student/ and applicaitonbase/api/student

Upvotes: 5

von v.
von v.

Reputation: 17108

Make you base controller inherit from the ApiController first, and then inherit your base controller from your student controller.

public class BaseController : ApiController

public class StudentController : BaseController 

Upvotes: 1

Related Questions