/// <summary> /// Adds all <see cref="Controller"/> types found in the given assembly that /// are annotated with a <see cref="PrefixAttribute"/>. /// </summary> /// <param name="assembly">Assembly to search for <see cref="Controller"/> types.</param> public void Add(Assembly assembly) { foreach (var type in assembly.GetTypes()) { if (!typeof(Controller).IsAssignableFrom(type)) { continue; } var attribs = type.GetCustomAttributes <PrefixAttribute>().AsArray(); if (attribs.Length == 0) { continue; } var ctor = type.GetConstructor(Type.EmptyTypes); if (ctor == null) { continue; } var ctorCall = Expression.New(ctor); var lambda = Expression.Lambda <Func <Controller> >(ctorCall).Compile(); foreach (var attrib in attribs) { var matcher = UrlMatcher.Parse(attrib.Value, attrib.Extension); Add(matcher, attrib.Priority, type, lambda); } } }
internal void SetMatchedActionUrl(UrlMatcher matcher, UrlMatch match) { _actionMatcher = matcher; _actionMatch = match; _urlMatcher = new ConcatenatedPrefixMatcher(ControllerMatcher, ActionMatcher); }
internal void Initialize(UrlMatcher matcher, Server server) { ControllerMatcher = matcher; Server = server; LastRequestTime = DateTime.UtcNow; }
public BoundController(UrlMatcher matcher, float priority, Type controllerType, Func <Controller> ctor) { Matcher = matcher; Ctor = ctor; _priority = priority; _controllerType = controllerType; }
public BoundAction(UrlMatcher matcher, bool matchAllUrl, float priority, HttpMethod httpMethod, ControllerAction action, string name) { Matcher = matcher; MatchAllUrl = matchAllUrl; HttpMethod = httpMethod; Action = action; _priority = priority; _actionName = name; }
/// <summary> /// Maps the given controller constructor to the given <see cref="UrlMatcher"/> with a specific priority. /// </summary> /// <param name="matcher"><see cref="UrlMatcher"/> that will map to the given controller constructor.</param> /// <param name="priority">Priority used when sorting controllers.</param> /// <param name="controllerType">Type of the controller constructor to map.</param> /// <param name="ctor">Controller constructor to map.</param> public void Add(UrlMatcher matcher, float priority, Type controllerType, Func <Controller> ctor) { if (float.IsNaN(priority)) { var defaultPriority = controllerType.GetCustomAttribute <DefaultPriorityAttribute>(); priority = defaultPriority == null ? 0f : defaultPriority.Value; } _controllers.Add(new BoundController(matcher, priority, controllerType, ctor)); _sorted = false; }
internal bool TryGetController(UrlMatcher matcher, out Controller controller) { lock (this) { foreach (var active in _controllers) { if (active.ControllerMatcher != matcher) { continue; } controller = active; return(true); } } controller = null; return(false); }
private ControllerActionMap(Type controllerType) { ControllerType = controllerType; foreach (var method in ControllerType.GetMethods(InstanceFlags)) { var attribs = method.GetCustomAttributes <ControllerActionAttribute>(true).AsArray(); foreach (var attrib in attribs) { var action = GenerateAction(method, attrib); var matcher = UrlMatcher.Parse(attrib.Value, attrib.Extension); var httpMethod = attrib is GetAttribute ? HttpMethod.Get : attrib is PostAttribute ? HttpMethod.Post : null; _actions.Add(new BoundAction(matcher, attrib.MatchAllUrl, attrib.Priority, httpMethod, action, method.Name)); } } _actions.Sort(); }
/// <summary> /// Maps the given controller constructor to the given <see cref="UrlMatcher"/>. /// </summary> /// <typeparam name="TController">Type of the controller constructor to map.</typeparam> /// <param name="matcher"><see cref="UrlMatcher"/> that will map to the given controller constructor.</param> /// <param name="ctor">Controller constructor to map.</param> public void Add <TController>(UrlMatcher matcher, Func <TController> ctor) where TController : Controller { Add(matcher, float.NaN, ctor); }
/// <summary> /// Maps the given controller constructor to the given <see cref="UrlMatcher"/> with a specific priority. /// </summary> /// <typeparam name="TController">Type of the controller constructor to map.</typeparam> /// <param name="matcher"><see cref="UrlMatcher"/> that will map to the given controller constructor.</param> /// <param name="priority">Priority used when sorting controllers.</param> /// <param name="ctor">Controller constructor to map.</param> public void Add <TController>(UrlMatcher matcher, float priority, Func <TController> ctor) where TController : Controller { Add(matcher, priority, typeof(TController), ctor); }
/// <summary> /// Maps the default constructor of the given controller type to the given /// <see cref="UrlMatcher"/> with an optional priority. /// </summary> /// <typeparam name="TController">Type of the controller constructor to map.</typeparam> /// <param name="matcher"><see cref="UrlMatcher"/> that will map to the given controller constructor.</param> /// <param name="priority">Optional priority used when sorting controllers.</param> public void Add <TController>(UrlMatcher matcher, float priority = float.NaN) where TController : Controller, new() { Add(matcher, priority, typeof(TController), () => new TController()); }
public ConcatenatedPrefixMatcher(UrlMatcher first, UrlMatcher second) { _first = first; _second = second; }