public IEnumerable <Endpoint> FindEndpoints(RouteValuesAddress address)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            // Run custom tenant schemes.
            foreach (var scheme in _schemes)
            {
                var endpoints = scheme.FindEndpoints(address);

                if (endpoints.Any())
                {
                    return(endpoints);
                }
            }

            if (!_defaultSchemeInitialized)
            {
                lock (this)
                {
                    // Try once to get and cache the default scheme but not me.
                    _defaultScheme = _httpContextAccessor.HttpContext?.RequestServices
                                     .GetServices <IEndpointAddressScheme <RouteValuesAddress> >()
                                     .Where(scheme => scheme.GetType() != GetType())
                                     .LastOrDefault();

                    _defaultSchemeInitialized = true;
                }
            }

            // Fallback to the default 'RouteValuesAddress' scheme.
            return(_defaultScheme?.FindEndpoints(address) ?? Enumerable.Empty <Endpoint>());
        }
        public IEnumerable<Endpoint> FindEndpoints(RouteValuesAddress address)
        {
            var endpoints = _inner.FindEndpoints(address);

            if (ShouldTryCultureNeutralEndpoint(address, endpoints))
            {
                // Link generation by route name fails, because we actually create two named routes:
                // 1 = {RouteName}, 2 = {RouteName}__noculture (the latter for code-less default language according to settings).
                // In case the current request is in default culture and "DefaultLanguageRedirectBehaviour.StripSeoCode"
                // is set, we have to refer to {RouteName}__noculture, not {RouteName}, otherwise culture code gets appended as querystring.
                address.RouteName += "__noculture";
                endpoints = _inner.FindEndpoints(address);
            }

            return endpoints;
        }
Esempio n. 3
0
    private static void PrepForReExecute(HttpContext context, UploadErrorsModel uploadErrorsModel)
    {
        // Clear request
        context.Request.Method = "POST";
        context.Request.Body   = Stream.Null;

        if (uploadErrorsModel.Errors?.Length > 0)
        {
            Dictionary <string, StringValues> query = new Dictionary <string, StringValues> {
                [nameof(uploadErrorsModel.Errors)] = uploadErrorsModel.Errors
            };
            context.Request.QueryString = QueryString.Create(query);
        }
        else
        {
            context.Request.QueryString = new QueryString();
        }

        // Reset the request in its most basic form
        context.Request.ContentType = "text/plain";
        context.Request.PathBase    = "/";
        context.Request.Path        = "/";

        // Set endpoint and associated routing data
        const string continuationEndpoint         = "AfterUploadCompletionFrame";
        IEndpointAddressScheme <string> svc       = context.RequestServices.GetRequiredService <IEndpointAddressScheme <string> >();
        IEnumerable <Endpoint>          endpoints = svc.FindEndpoints(continuationEndpoint);

        bool hasSetEndpoint = false;

        foreach (Endpoint endpoint in endpoints)
        {
            if (hasSetEndpoint)
            {
                throw new InvalidOperationException($"Multiple endpoints {continuationEndpoint}");
            }

            // Prepare routing values MVC uses for view lookups
            ControllerActionDescriptor?actionDescriptor = endpoint.Metadata.GetMetadata <ControllerActionDescriptor>();
            if (actionDescriptor != null)
            {
                RouteData routeData = context.GetRouteData();

                foreach (KeyValuePair <string, string?> routeValue in actionDescriptor.RouteValues)
                {
                    routeData.Values[routeValue.Key] = routeValue.Value;
                }
            }

            // Set to endpoint to use later
            context.SetEndpoint(endpoint);
            hasSetEndpoint = true;
        }

        if (!hasSetEndpoint)
        {
            throw new InvalidOperationException($"Unable to find continuation endpoint {continuationEndpoint}");
        }
    }
Esempio n. 4
0
    public IActionResult GetMetadata()
    {
        var metadata = _metadataProvider.GetMetadata();
        // EXPECTED: "api/metadata/{name}"
        // ACTUAL: "api/metadata/{name}"
        string actionName     = nameof(MetadataController.GetById);
        string controllerName = nameof(MetadataController).Replace(nameof(Controller), string.Empty);
        var    url            = _endpointAddress.FindEndpoints(CreateAddress(actionName, controllerName))
                                .OfType <RouteEndpoint>()
                                .Select(x => x.RoutePattern)
                                .FirstOrDefault();;

        var response = new HALResponse(metadata)
                       .AddSelfLink(HttpContext.Request)
                       .AddLinks(new Link(name, url));

        return(Ok(response));
    }