Created
September 20, 2017 19:23
-
-
Save d2funlife/19107e869e06072360a8b8556da3b4ef to your computer and use it in GitHub Desktop.
DefaultControllerActivator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class DefaultControllerActivator : IControllerActivator | |
{ | |
private Func<IDependencyResolver> _resolverThunk; | |
public DefaultControllerActivator() | |
: this(null) | |
{ | |
} | |
public DefaultControllerActivator(IDependencyResolver resolver) | |
{ | |
if (resolver == null) | |
{ | |
_resolverThunk = () => DependencyResolver.Current; | |
} | |
else | |
{ | |
_resolverThunk = () => resolver; | |
} | |
} | |
public IController Create(RequestContext requestContext, Type controllerType) | |
{ | |
try | |
{ | |
return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType)); | |
} | |
catch (Exception ex) | |
{ | |
throw new InvalidOperationException( | |
String.Format( | |
CultureInfo.CurrentCulture, | |
MvcResources.DefaultControllerFactory_ErrorCreatingController, | |
controllerType), | |
ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment