public override string Action(UrlActionContext urlActionContext)
    {
        if (urlActionContext == null)
        {
            throw new ArgumentNullException(nameof(urlActionContext));
        }

        var values = GetValuesDictionary(urlActionContext.Values);

        if (urlActionContext.Action != null)
        {
            values["action"] = urlActionContext.Action;
        }
        else if (!values.ContainsKey("action") && AmbientValues.TryGetValue("action", out var action))
        {
            values["action"] = action;
        }

        if (urlActionContext.Controller != null)
        {
            values["controller"] = urlActionContext.Controller;
        }
        else if (!values.ContainsKey("controller") && AmbientValues.TryGetValue("controller", out var controller))
        {
            values["controller"] = controller;
        }

        var path = _linkGenerator.GetPathByRouteValues(
            ActionContext.HttpContext,
            routeName: null,
            values,
            fragment: new FragmentString(urlActionContext.Fragment == null ? null : "#" + urlActionContext.Fragment));

        return(GenerateUrl(urlActionContext.Protocol, urlActionContext.Host, path));
    }
Beispiel #2
0
        /// <inheritdoc />
        public override string Action(UrlActionContext urlActionContext)
        {
            if (urlActionContext == null)
            {
                throw new ArgumentNullException(nameof(urlActionContext));
            }

            var valuesDictionary = GetValuesDictionary(urlActionContext.Values);

            if (urlActionContext.Action == null)
            {
                if (!valuesDictionary.ContainsKey("action") &&
                    AmbientValues.TryGetValue("action", out var action))
                {
                    valuesDictionary["action"] = action;
                }
            }
            else
            {
                valuesDictionary["action"] = urlActionContext.Action;
            }

            if (urlActionContext.Controller == null)
            {
                if (!valuesDictionary.ContainsKey("controller") &&
                    AmbientValues.TryGetValue("controller", out var controller))
                {
                    valuesDictionary["controller"] = controller;
                }
            }
            else
            {
                valuesDictionary["controller"] = urlActionContext.Controller;
            }

            var endpoints = _routeValuesBasedEndpointFinder.FindEndpoints(
                new RouteValuesBasedEndpointFinderContext()
            {
                ExplicitValues = valuesDictionary,
                AmbientValues  = AmbientValues
            });

            var successfullyGeneratedLink = _linkGenerator.TryGetLink(
                ActionContext.HttpContext,
                endpoints,
                valuesDictionary,
                AmbientValues,
                out var link);

            if (!successfullyGeneratedLink)
            {
                //TODO: log here

                return(null);
            }

            return(GenerateUrl(urlActionContext.Protocol, urlActionContext.Host, link, urlActionContext.Fragment));
        }
Beispiel #3
0
        public override string Action(UrlActionContext actionContext)
        {
            var nonRoundTripUsingQueryStringValues = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase);

            var values = GetValuesDictionary(actionContext.Values);

            foreach (var routeDataStringKey in _options.AmbientRouteDataKeys)
            {
                if (!values.ContainsKey(routeDataStringKey.RouteDataKey) &&
                    AmbientValues.TryGetValue(routeDataStringKey.RouteDataKey, out var value))
                {
                    if (!routeDataStringKey.RoundTripUsingQueryString)
                    {
                        nonRoundTripUsingQueryStringValues.Add(routeDataStringKey.RouteDataKey, value);
                    }

                    values[routeDataStringKey.RouteDataKey] = value;
                }
                else if (!values.ContainsKey(routeDataStringKey.RouteDataKey) && _urlHelper.ActionContext.HttpContext.Request.Query.TryGetValue(routeDataStringKey.RouteDataKey, out var queryStringValues))
                {
                    if (!routeDataStringKey.RoundTripUsingQueryString)
                    {
                        nonRoundTripUsingQueryStringValues.Add(routeDataStringKey.RouteDataKey, queryStringValues.First());
                    }

                    values[routeDataStringKey.RouteDataKey] = queryStringValues.First();
                }
            }

            actionContext.Values = values;

            var url = _urlHelper.Action(actionContext);

            if (url != null)
            {
                var uri = new Uri(url, UriKind.RelativeOrAbsolute);
                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri($"http://www.domain.com{url}");
                }

                var queryDictionary = QueryHelpers.ParseQuery(uri.Query);

                if (queryDictionary.Keys.Any(k => nonRoundTripUsingQueryStringValues.ContainsKey(k)))
                {
                    foreach (var key in queryDictionary.Keys.Where(k => nonRoundTripUsingQueryStringValues.ContainsKey(k)))
                    {
                        values.Remove(key);
                    }

                    actionContext.Values = values;
                    url = _urlHelper.Action(actionContext);
                }
            }

            return(url);
        }
Beispiel #4
0
        /// <inheritdoc />
        public override string Action(UrlActionContext actionContext)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }

            var valuesDictionary = GetValuesDictionary(actionContext.Values);

            if (actionContext.Action == null)
            {
                if (!valuesDictionary.ContainsKey("action") &&
                    AmbientValues.TryGetValue("action", out object action))
                {
                    valuesDictionary["action"] = action;
                }
            }
            else
            {
                valuesDictionary["action"] = actionContext.Action;
            }

            if (actionContext.Controller == null)
            {
                if (!valuesDictionary.ContainsKey("controller") &&
                    AmbientValues.TryGetValue("controller", out object controller))
                {
                    valuesDictionary["controller"] = controller;
                }
            }
            else
            {
                valuesDictionary["controller"] = actionContext.Controller;
            }

            var pathData = GetVirtualPathData(routeName: null, values: valuesDictionary);

            if (pathData is AbsolutPathData)
            {
                var absolutePathData = pathData as AbsolutPathData;

                if (string.Equals(absolutePathData.Host, HttpContext.Request.Host.Value, StringComparison.CurrentCultureIgnoreCase) && absolutePathData.Protocol == HttpContext.Request.Scheme)
                {
                    return(GenerateUrl(null, null, pathData, actionContext.Fragment));
                }
                //we don't support changing protocol for subdomain
                return(GenerateUrl(absolutePathData.Protocol, absolutePathData.Host, pathData, actionContext.Fragment));
            }
            return(GenerateUrl(actionContext.Protocol, actionContext.Host, pathData, actionContext.Fragment));
        }
Beispiel #5
0
        private string GetActionValue(UrlActionContext urlActionContext, RouteValueDictionary values)
        {
            string action = null;

            if (urlActionContext.Action != null)
            {
                action = urlActionContext.Action;
            }
            else if (!values.ContainsKey(RouteValue.Action) &&
                     AmbientValues.TryGetValue(RouteValue.Action, out var actionValue))
            {
                action = (string)actionValue;
            }

            return(action?.ToLower());
        }
Beispiel #6
0
        private string GetControllerValue(UrlActionContext urlActionContext, RouteValueDictionary values)
        {
            string controller = null;

            if (urlActionContext.Controller != null)
            {
                controller = urlActionContext.Controller;
            }
            else if (!values.ContainsKey(RouteValue.Controller) &&
                     AmbientValues.TryGetValue(RouteValue.Controller, out var controllerValue))
            {
                controller = (string)controllerValue;
            }

            return(controller?.ToLower());
        }
Beispiel #7
0
        private string GetCultureValue(RouteValueDictionary values)
        {
            var currentCulture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToLower();

            if (AmbientValues.TryGetValue(RouteValue.Culture, out var ambiantCulture))
            {
                currentCulture = (string)ambiantCulture;
            }

            if (values.TryGetValue(RouteValue.Culture, out var culture))
            {
                currentCulture = (string)culture;
            }

            return(currentCulture);
        }
Beispiel #8
0
        /// <inheritdoc />
        public override string Action(UrlActionContext actionContext)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }

            var valuesDictionary = GetValuesDictionary(actionContext.Values);

            if (actionContext.Action == null)
            {
                if (!valuesDictionary.ContainsKey("action") &&
                    AmbientValues.TryGetValue("action", out var action))
                {
                    valuesDictionary["action"] = action;
                }
            }
            else
            {
                valuesDictionary["action"] = actionContext.Action;
            }

            if (actionContext.Controller == null)
            {
                if (!valuesDictionary.ContainsKey("controller") &&
                    AmbientValues.TryGetValue("controller", out var controller))
                {
                    valuesDictionary["controller"] = controller;
                }
            }
            else
            {
                valuesDictionary["controller"] = actionContext.Controller;
            }

            var virtualPathData = GetVirtualPathData(routeName: null, values: valuesDictionary);

            return(GenerateUrl(actionContext.Protocol, actionContext.Host, virtualPathData, actionContext.Fragment));
        }
Beispiel #9
0
        public override string Action(UrlActionContext urlActionContext)
        {
            if (urlActionContext == null)
            {
                throw new ArgumentNullException(nameof(urlActionContext));
            }

            var values         = GetValuesDictionary(urlActionContext.Values);
            var currentCulture = GetCultureValue(values);

            if (!string.IsNullOrEmpty(currentCulture))
            {
                values[RouteValue.Culture] = currentCulture;
            }

            var controllerValue = GetControllerValue(urlActionContext, values);

            if (!string.IsNullOrEmpty(controllerValue))
            {
                values[RouteValue.Controller] = _routeService.GetControllerTranslatedValue(controllerValue, currentCulture);
            }

            var actionValue = GetActionValue(urlActionContext, values);

            if (!string.IsNullOrEmpty(actionValue))
            {
                values[RouteValue.Action] = _routeService.GetActionTranslatedValue(controllerValue, actionValue, currentCulture);
            }

            if (urlActionContext.Controller == null && urlActionContext.Action == null)
            {
                if (AmbientValues.ContainsKey(RouteValue.Id) &&
                    AmbientValues.TryGetValue(RouteValue.Id, out var id))
                {
                    values[RouteValue.Id] = id;
                }
            }

            string path;

            var rules = _routeService.RouteRules.Where(r =>
                                                       r.ControllerName.Equals(controllerValue, StringComparison.OrdinalIgnoreCase)).ToList();

            var rule = rules.FirstOrDefault(r => r.ActionName.Equals(actionValue, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                rule = rules.FirstOrDefault(r => r.ActionName == null);
            }

            var fragment = urlActionContext.Fragment == null
                ? FragmentString.Empty
                : new FragmentString("#" + urlActionContext.Fragment);

            if (rule != null)
            {
                path = rule.GenerateUrlPathCallback(
                    values,
                    fragment);
            }
            else
            {
                path = _linkGenerator.GetPathByRouteValues(
                    ActionContext.HttpContext,
                    routeName: null,
                    values,
                    fragment: fragment);
            }



            return(GenerateUrl(urlActionContext.Protocol, urlActionContext.Host, path));
        }