Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I follow <a href="http://rads.stackoverflow.com/amzn/click/1430210079" rel="nofollow noreferrer">Steve Sanderson's</a> pattern in creating a WindsorControllerFactory, which works quite well. I've modified his to actually return proper 404s when there isn't a controller by the name (i.e. someone types "/garbageblahblah") because his pattern didn't pick it up. So, I use Reflector to grab the bits that are built into the default controller factory for handling bad urls.</p> <p>His pattern uses reflection to find all controllers within your Mvc project, and register them all upon app startup. You don't want to use CreateController, but instead GetControllerInstance() as this is only called by the underlying Mvc framework when a controller is going to be invoked. /Content is ignored by convention, and therefore is not called.</p> <p>My CastleWindsorControllerFactory:</p> <pre><code>/// &lt;summary&gt; /// Represents a special controller factory. /// &lt;/summary&gt; public class CastleWindsorControllerFactory : DefaultControllerFactory { WindsorContainer _container; public CastleWindsorControllerFactory() { // register all controllers from the calling assembly. // (e.g. the mvc site calling this factory) // _container = new WindsorContainer( new XmlInterpreter( new ConfigResource("castle") ) ); // change this to Assembly.GetAssembly() if used directly from // your MVC website. The code below is for when this class // exists in a seperate assembly. // var controllers = from t in Assembly.GetCallingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t; foreach (Type t in controllers) _container.AddComponentLifeStyle( t.FullName, t, LifestyleType.Transient); } protected override IController GetControllerInstance(Type controllerType) { if (controllerType == null) { throw new HttpException( 0x194 , string.Format( CultureInfo.CurrentUICulture , "Controller Not Found" , new object[] { this.RequestContext.HttpContext.Request.Path })); } if (false == typeof(IController).IsAssignableFrom(controllerType)) { throw new ArgumentException( string.Format( CultureInfo.CurrentUICulture , "Type does not sub-class the controller base" , new object[] { controllerType }), "controllerType"); } return (IController) _container.Resolve(controllerType); } } </code></pre> <p>And within my Globals.asax.cs, this is all you need (what you have above is vast overkill! I already loop through and register them within my CastleWindsorControllerFactor above).</p> <pre><code>protected void Application_Start() { RegisterRoutes(RouteTable.Routes); // custom controller factory that uses Windsor ControllerBuilder.Current.SetControllerFactory( new CastleWindsorControllerFactory()); // Uncomment to debug routes //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); } </code></pre> <p>Technically, this isn't exactly the code I use. I have actually abstracted the entire Castle Windsor off to a static instance in a class I call ComponentFactory. This allows me to have true Singleton patterns through my applications, amongst several strong-type additions.</p> <p>I mention this, because I plan on releasing ComponentFactory() soon on my blog. But, drop me an email and I'll send the latest version to you. Email is: me -at- eduncan911.com</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload