Example #1
0
        protected override HttpControllerDescriptor OnSelectController(HttpRequestMessage request)
        {
            ControllerIdentification cName      = this.GetControllerIdentificationFromRequest(request);
            var    majorVersion                 = cName.Version.ToString().Split('.')[0];
            var    minorVersion                 = cName.Version.ToString().Split('.')[1];
            var    prefixNamespace              = "mydocumentalapi.controllers.";
            string requestVersionMajorPlusMinor = string.Format("{0}version{1}_{2}", prefixNamespace, majorVersion, minorVersion);
            string requestVersion               = string.Format("{0}version{1}", prefixNamespace, majorVersion);
            string defaultVersion               = string.Format("{0}version0", prefixNamespace);

            String   controllerName = cName.Name;
            Assembly assembly       = Assembly.GetExecutingAssembly();


            var controllerWithExactMajorMinorVersion = assembly
                                                       .GetTypes()
                                                       .FirstOrDefault(i => typeof(IHttpController).IsAssignableFrom(i) &&
                                                                       i.Name.ToLower().Equals(string.Format("{0}controller", cName.Name.ToLower())) &&
                                                                       i.Namespace.ToLower().Equals(requestVersionMajorPlusMinor)
                                                                       );

            // Esiste controller con versione Major+Minor esatta
            if (controllerWithExactMajorMinorVersion != null)
            {
                return(new HttpControllerDescriptor(_configuration, controllerName, controllerWithExactMajorMinorVersion));
            }
            // Se la minor รจ 0 cerco la major
            if (minorVersion == "0")
            {
                var controllerWithExactVersion = assembly
                                                 .GetTypes()
                                                 .FirstOrDefault(i => typeof(IHttpController).IsAssignableFrom(i) &&
                                                                 i.Name.ToLower().Equals(string.Format("{0}controller", cName.Name.ToLower())) &&
                                                                 i.Namespace.ToLower().Equals(requestVersion)
                                                                 );
                if (controllerWithExactVersion != null)
                {
                    return(new HttpControllerDescriptor(_configuration, controllerName, controllerWithExactVersion));
                }
            }

            Type defaultControllerType = assembly.GetTypes()
                                         .Where(i => typeof(IHttpController).IsAssignableFrom(i))
                                         .FirstOrDefault(i => i.Name.ToLower() == string.Format("{0}controller", cName.Name.ToLower()) &&
                                                         i.Namespace.ToLower().Equals(defaultVersion));

            return(new HttpControllerDescriptor(_configuration, controllerName, defaultControllerType));
        }
        public ICollection<Type> GetControllerTypes(ControllerIdentification controllerName) {
            if (String.IsNullOrEmpty(controllerName.Name)) {
                throw new ArgumentNullException("controllerName");
            }

            var matchingTypes = new HashSet<Type>();

            ILookup<string, Type> namespaceLookup;
            if (this._cache.Value.TryGetValue(controllerName, out namespaceLookup)) {
                foreach (var namespaceGroup in namespaceLookup) {
                    matchingTypes.UnionWith(namespaceGroup);
                }
            }

            return matchingTypes;
        }
        /// <summary>
        /// Creates a default exception
        /// </summary>
        /// <returns></returns>
        public static AmbigiousApiRequestException Create(ControllerIdentification controllerIdentification, IHttpRoute route, IEnumerable <Type> matchingTypes)
        {
            Contract.Assert(route != null);
            Contract.Assert(controllerIdentification != null);
            Contract.Assert(matchingTypes != null);

            // Generate an exception containing all the controller types
            StringBuilder typeList = new StringBuilder();

            foreach (Type matchedType in matchingTypes)
            {
                typeList.AppendLine();
                typeList.Append(matchedType.FullName);
            }

            return(new AmbigiousApiRequestException(
                       String.Format(ExceptionResources.AmbigiousControllerRequest, controllerIdentification, route.RouteTemplate, typeList),
                       controllerIdentification,
                       route,
                       matchingTypes));
        }
        public ICollection <Type> GetControllerTypes(ControllerIdentification controllerId)
        {
            if (controllerId == null)
            {
                throw new ArgumentNullException("controllerId");
            }

            var matchingTypes = new HashSet <Type>();

            ILookup <string, Type> namespaceLookup;

            if (this._cache.Value.TryGetValue(controllerId, out namespaceLookup))
            {
                foreach (var namespaceGroup in namespaceLookup)
                {
                    matchingTypes.UnionWith(namespaceGroup);
                }
            }

            return(matchingTypes);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Exception"/> class.
 /// </summary>
 public AmbigiousApiRequestException(string message, ControllerIdentification controllerIdentification, IHttpRoute route, IEnumerable <Type> matchingTypes) : base(message)
 {
     this._controllerIdentification = controllerIdentification;
     this._route         = route;
     this._matchingTypes = matchingTypes;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Exception"/> class with serialized data.
 /// </summary>
 /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown. </param><param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination. </param><exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception><exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
 protected AmbigiousApiRequestException(SerializationInfo info, StreamingContext context) : base(info, context)
 {
     this._controllerIdentification = (ControllerIdentification)info.GetValue("_controllerIdentification", typeof(ControllerIdentification));
     this._matchingTypes            = (IEnumerable <Type>)info.GetValue("_matchingTypes", typeof(List <Type>));
 }
Example #7
0
 /// <summary>
 /// Creates a default exception
 /// </summary>
 /// <returns></returns>
 public static ApiControllerNotFoundException Create(ControllerIdentification controllerIdentification)
 {
     return(new ApiControllerNotFoundException(String.Format(ExceptionResources.ApiControllerNotFound, controllerIdentification), controllerIdentification));
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Exception"/> class with a specified error message.
 /// </summary>
 /// <param name="message">The message that describes the error. </param>
 /// <param name="controllerIdentification"></param>
 public ApiControllerNotFoundException(string message, ControllerIdentification controllerIdentification) : base(message)
 {
     this._controllerIdentification = controllerIdentification;
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Exception"/> class with serialized data.
 /// </summary>
 /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown. </param><param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination. </param><exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception><exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
 protected ApiControllerNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
 {
     this._controllerIdentification = (ControllerIdentification)info.GetValue("_controllerIdentification", typeof(ControllerIdentification));
 }
 /// <summary>
 /// Creates a default exception 
 /// </summary>
 /// <returns></returns>
 public static ApiControllerNotFoundException Create(ControllerIdentification controllerIdentification) {
     return new ApiControllerNotFoundException(String.Format(ExceptionResources.ApiControllerNotFound, controllerIdentification), controllerIdentification);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Exception"/> class with a specified error message.
 /// </summary>
 /// <param name="message">The message that describes the error. </param>
 /// <param name="controllerIdentification"></param>
 public ApiControllerNotFoundException(string message, ControllerIdentification controllerIdentification) : base(message) {
     this._controllerIdentification = controllerIdentification;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Exception"/> class with serialized data.
 /// </summary>
 /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown. </param><param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination. </param><exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception><exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
 protected ApiControllerNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) {
     this._controllerIdentification = (ControllerIdentification) info.GetValue("_controllerIdentification", typeof (ControllerIdentification));
 }