protected void Application_Start() { RegisterRoutes(RouteTable.Routes); // Add the following line to make sure your CustomerControllerFactory gets used ControllerBuilder.Current.SetControllerFactory(typeof(Acme.Web.Common.CustomControllerFactory)); ... }
public class CustomControllerFactory : IControllerFactory { public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { if (string.IsNullOrEmpty(controllerName)) throw new ArgumentNullException("controllerName"); IController controller; string cTypeName = "Acme.Web.Controllers." + controllerName + "Controller"; var cType = Type.GetType(cTypeName); if (cType != null) { controller = Activator.CreateInstance(cType) as IController; return controller; } var assemblyPath = requestContext.HttpContext.Server.MapPath("~"); // For simplicity sake, we hard code the name of the assembly, but you can // implement whatever logic you want here, possibly pulling from a configuration file. assemblyPath = Path.Combine(assemblyPath, @"bin\MyControllers.DLL"); var assembly = Assembly.LoadFrom(assemblyPath); var c = assembly.CreateInstance(cTypeName); controller = c as IController; return controller; } public void ReleaseController(IController controller) { if (controller is IDisposable) (controller as IDisposable).Dispose(); else controller = null; } }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.