// // Authors: Nate Kohari , Remo Gloor // Copyright (c) 2007-2010, Enkari, Ltd. and contributors // // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). // See the file LICENSE.txt for details. // namespace Ninject.Web.Mvc { using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Ninject.Activation; using Ninject.Activation.Providers; using Ninject.Components; using Ninject.Infrastructure; using Ninject.Parameters; using Ninject.Planning.Bindings; using Ninject.Planning.Bindings.Resolvers; /// /// Missing binding resolver that creates a binding for unknown controllers. /// public class ControllerMissingBindingResolver : NinjectComponent, IMissingBindingResolver { /// /// Returns any bindings from the specified collection that match the specified request. /// /// The multimap of all registered bindings. /// The request in question. /// The series of matching bindings. public IEnumerable Resolve(Multimap bindings, IRequest request) { var service = request.Service; if (typeof(Controller).IsAssignableFrom(service)) { var binding = new Binding(service) { ProviderCallback = StandardProvider.GetCreationCallback(service) }; binding.Parameters.Add( typeof(AsyncController).IsAssignableFrom(service) ? new PropertyValue("ActionInvoker", ctx => ctx.Kernel.Get()) : new PropertyValue("ActionInvoker", ctx => ctx.Kernel.Get())); return new[] { binding }; } return Enumerable.Empty(); } } }