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?
-
maybe this can help your: stackoverflow.com/questions/9557909/…freshbm– freshbm2015年07月15日 14:22:20 +00:00Commented Jul 15, 2015 at 14:22
1 Answer 1
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()
Comments
Explore related questions
See similar questions with these tags.