MvcRouteHandler是默认的标准路由处理器类,它将创建一个MvcHandler实例对象,而MvcHandler又是默认的MVC Handler处理类
它只有一个构造器,且要传递RequestContext类型作为参数,因此它是不能注册到站点的处理程序列表中的。
MVC模式下,实现IControllerFactory接口的类来获取当前的请求控制器对象
而 MvcHandler 通过ControllerBuilder类来获取实现IControllerFactory接口的控制器工厂
详细如下
MvcHandler的ProcessRequestInit方法代码:
private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
{
if (ValidationUtility.IsValidationEnabled(HttpContext.Current) == true)
{
ValidationUtility.EnableDynamicValidation(HttpContext.Current);
}
this.AddVersionHeader(httpContext);
this.RemoveOptionalRoutingParameters();
string requiredString = this.RequestContext.get_RouteData().GetRequiredString("controller");
factory = this.ControllerBuilder.GetControllerFactory();
controller = factory.CreateController(this.RequestContext, requiredString);
if (controller == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_FactoryReturnedNull, new object[] { factory.GetType(), requiredString }));
}
}
{
if (ValidationUtility.IsValidationEnabled(HttpContext.Current) == true)
{
ValidationUtility.EnableDynamicValidation(HttpContext.Current);
}
this.AddVersionHeader(httpContext);
this.RemoveOptionalRoutingParameters();
string requiredString = this.RequestContext.get_RouteData().GetRequiredString("controller");
factory = this.ControllerBuilder.GetControllerFactory();
controller = factory.CreateController(this.RequestContext, requiredString);
if (controller == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_FactoryReturnedNull, new object[] { factory.GetType(), requiredString }));
}
}
IControllerFactory接口:
public interface IControllerFactory
{
// Methods
IController CreateController(RequestContext requestContext, string controllerName);
SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName);
void ReleaseController(IController controller);
}
{
// Methods
IController CreateController(RequestContext requestContext, string controllerName);
SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName);
void ReleaseController(IController controller);
}
DefaultControllerFactory:
View Code
public class DefaultControllerFactory : IControllerFactory
{
// Fields
private IResolver<IControllerActivator> _activatorResolver;
private IBuildManager _buildManager;
private IControllerActivator _controllerActivator;
private ControllerBuilder _controllerBuilder;
private ControllerTypeCache _instanceControllerTypeCache;
private static readonly ConcurrentDictionary<Type, SessionStateBehavior> _sessionStateCache;
private static ControllerTypeCache _staticControllerTypeCache;
// Methods
static DefaultControllerFactory();
public DefaultControllerFactory();
public DefaultControllerFactory(IControllerActivator controllerActivator);
internal DefaultControllerFactory(IControllerActivator controllerActivator, IResolver<IControllerActivator> activatorResolver, IDependencyResolver dependencyResolver);
internal static InvalidOperationException CreateAmbiguousControllerException(RouteBase route, string controllerName, ICollection<Type> matchingTypes);
public virtual IController CreateController(RequestContext requestContext, string controllerName);
protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType);
protected internal virtual SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, Type controllerType);
protected internal virtual Type GetControllerType(RequestContext requestContext, string controllerName);
private Type GetControllerTypeWithinNamespaces(RouteBase route, string controllerName, HashSet<string> namespaces);
public virtual void ReleaseController(IController controller);
SessionStateBehavior IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, string controllerName);
// Properties
internal IBuildManager BuildManager { get; set; }
private IControllerActivator ControllerActivator { get; }
internal ControllerBuilder ControllerBuilder { get; set; }
internal ControllerTypeCache ControllerTypeCache { get; set; }
// Nested Types
private class DefaultControllerActivator : IControllerActivator
{
// Fields
private Func<IDependencyResolver> _resolverThunk;
// Methods
public DefaultControllerActivator();
public DefaultControllerActivator(IDependencyResolver resolver);
public IController Create(RequestContext requestContext, Type controllerType);
}
}
{
// Fields
private IResolver<IControllerActivator> _activatorResolver;
private IBuildManager _buildManager;
private IControllerActivator _controllerActivator;
private ControllerBuilder _controllerBuilder;
private ControllerTypeCache _instanceControllerTypeCache;
private static readonly ConcurrentDictionary<Type, SessionStateBehavior> _sessionStateCache;
private static ControllerTypeCache _staticControllerTypeCache;
// Methods
static DefaultControllerFactory();
public DefaultControllerFactory();
public DefaultControllerFactory(IControllerActivator controllerActivator);
internal DefaultControllerFactory(IControllerActivator controllerActivator, IResolver<IControllerActivator> activatorResolver, IDependencyResolver dependencyResolver);
internal static InvalidOperationException CreateAmbiguousControllerException(RouteBase route, string controllerName, ICollection<Type> matchingTypes);
public virtual IController CreateController(RequestContext requestContext, string controllerName);
protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType);
protected internal virtual SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, Type controllerType);
protected internal virtual Type GetControllerType(RequestContext requestContext, string controllerName);
private Type GetControllerTypeWithinNamespaces(RouteBase route, string controllerName, HashSet<string> namespaces);
public virtual void ReleaseController(IController controller);
SessionStateBehavior IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, string controllerName);
// Properties
internal IBuildManager BuildManager { get; set; }
private IControllerActivator ControllerActivator { get; }
internal ControllerBuilder ControllerBuilder { get; set; }
internal ControllerTypeCache ControllerTypeCache { get; set; }
// Nested Types
private class DefaultControllerActivator : IControllerActivator
{
// Fields
private Func<IDependencyResolver> _resolverThunk;
// Methods
public DefaultControllerActivator();
public DefaultControllerActivator(IDependencyResolver resolver);
public IController Create(RequestContext requestContext, Type controllerType);
}
}
ControllerBuilder:
public class ControllerBuilder
{
// Fields
private Func<IControllerFactory> _factoryThunk;
private static ControllerBuilder _instance;
private HashSet<string> _namespaces;
private IResolver<IControllerFactory> _serviceResolver;
// Methods
static ControllerBuilder();
public ControllerBuilder();
internal ControllerBuilder(IResolver<IControllerFactory> serviceResolver);
public IControllerFactory GetControllerFactory();
public void SetControllerFactory(Type controllerFactoryType);
public void SetControllerFactory(IControllerFactory controllerFactory);
// Properties
public static ControllerBuilder Current { get; }
public HashSet<string> DefaultNamespaces { get; }
}
{
// Fields
private Func<IControllerFactory> _factoryThunk;
private static ControllerBuilder _instance;
private HashSet<string> _namespaces;
private IResolver<IControllerFactory> _serviceResolver;
// Methods
static ControllerBuilder();
public ControllerBuilder();
internal ControllerBuilder(IResolver<IControllerFactory> serviceResolver);
public IControllerFactory GetControllerFactory();
public void SetControllerFactory(Type controllerFactoryType);
public void SetControllerFactory(IControllerFactory controllerFactory);
// Properties
public static ControllerBuilder Current { get; }
public HashSet<string> DefaultNamespaces { get; }
}
具体流程如下:
MvcHander-->ControllerBuilder-->IControllerFactory-->Controller