1

I have a relatively large project created originally in MVC 4. But I would like to leverage OData functionality for table sorting etc.

At the moment I am using Windsor Castle to resolve my MVC Controllers. http://blog.devdave.com/2013/04/castle-windsor-in-aspnet-mvc-4.html

If I inherit a controller from ApiController - it doesn't get resolved because:

IController System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)

method is passed null controllerType.

What I would like to achieve is to simply return IQuerable and let the ApiController do the paging and filtering by OData standards.

public class OdataController : ApiController
{
 [EnableQuery]
 public IQueryable<Employee> Get()
 {
 using (var repo = _factory.CreateRepository())
 {
 return repo.FilterAsQueryable<Employee>(x => true);
 }
 }
}

Is it possible to achieve this in one project?

asked Jul 15, 2015 at 14:03
1

1 Answer 1

2

You certainly can use web api and mvc controllers in the same project and have them resolve dependencies via your Castle Windsor container.

However, web api controllers are not created from the same controller factory as mvc controllers.

You will need to create a class that implements the IDependencyResolver interface, from the System.Web.Http.Dependencies assembly. Have this class use your container.

The syntax on how you set the dependency resolver for WebApi depends on the version of WebApi that you are using.

WebApi 1

GlobalConfiguration.Configuration.DependencyResolver = new MyCastleWindsorResolver();

Please see this blog post

WebApi 2

var config = new HttpConfiguration();
// set up routing and other web api config and what have you
// ....
// register dependency resolver
config.DependencyResolver = new MyCastleWindsorResolver()
answered Jul 15, 2015 at 14:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.