コード例 #1
0
        public virtual HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            IHttpRouteData           routeData = request.GetRouteData();
            HttpControllerDescriptor controllerDescriptor;

            if (routeData != null)
            {
                IHttpRoute route = routeData.Route;
                if (route != null)
                {
                    controllerDescriptor = route.GetDirectRouteController();
                    if (controllerDescriptor != null)
                    {
                        return(controllerDescriptor);
                    }
                }
            }

            string controllerName = GetControllerName(request);

            if (String.IsNullOrEmpty(controllerName))
            {
                throw new HttpResponseException(request.CreateErrorResponse(
                                                    HttpStatusCode.NotFound,
                                                    Error.Format(SRResources.ResourceNotFound, request.RequestUri),
                                                    Error.Format(SRResources.ControllerNameNotFound, request.RequestUri)));
            }

            if (_controllerInfoCache.Value.TryGetValue(controllerName, out controllerDescriptor))
            {
                return(controllerDescriptor);
            }

            ICollection <Type> matchingTypes = _controllerTypeCache.GetControllerTypes(controllerName);

            // ControllerInfoCache is already initialized.
            Contract.Assert(matchingTypes.Count != 1);

            if (matchingTypes.Count == 0)
            {
                // no matching types
                throw new HttpResponseException(request.CreateErrorResponse(
                                                    HttpStatusCode.NotFound,
                                                    Error.Format(SRResources.ResourceNotFound, request.RequestUri),
                                                    Error.Format(SRResources.DefaultControllerFactory_ControllerNameNotFound, controllerName)));
            }
            else
            {
                // multiple matching types
                throw CreateAmbiguousControllerException(request.GetRouteData().Route, controllerName, matchingTypes);
            }
        }
コード例 #2
0
        public virtual IHttpController CreateController(HttpControllerContext controllerContext, string controllerName)
        {
            if (controllerContext == null)
            {
                throw Error.ArgumentNull("controllerContext");
            }

            if (String.IsNullOrEmpty(controllerName))
            {
                throw Error.ArgumentNullOrEmpty("controllerName");
            }

            HttpControllerDescriptor controllerDescriptor;

            if (_controllerInfoCache.Value.TryGetValue(controllerName, out controllerDescriptor))
            {
                // Create controller instance
                return(CreateInstance(controllerContext, controllerDescriptor));
            }

            ICollection <Type> matchingTypes = _controllerTypeCache.GetControllerTypes(controllerName);

            switch (matchingTypes.Count)
            {
            case 0:
                // no matching types
                throw new HttpResponseException(controllerContext.Request.CreateResponse(
                                                    HttpStatusCode.NotFound,
                                                    Error.Format(SRResources.DefaultControllerFactory_ControllerNameNotFound, controllerName)));

            case 1:
                // single matching type
                Type match = matchingTypes.First();

                // Add controller descriptor to cache
                controllerDescriptor = new HttpControllerDescriptor(_configuration, controllerName, match);
                _controllerInfoCache.Value.TryAdd(controllerName, controllerDescriptor);

                // Create controller instance
                return(CreateInstance(controllerContext, controllerDescriptor));

            default:
                // multiple matching types
                throw new HttpResponseException(controllerContext.Request.CreateResponse(
                                                    HttpStatusCode.InternalServerError,
                                                    CreateAmbiguousControllerExceptionMessage(controllerContext.RouteData.Route, controllerName, matchingTypes)));
            }
        }