Beispiel #1
0
        private RouteMatchedParam _completeParamsByRouteDefaultsWhenRouteValuesContainsValue(
            Type actionType, Type routeDefaultsType, Type targetType, string paramName
            )
        {
            RouteValueDictionary routeValues   = HttpContext.Current.Request.RequestContext.RouteData.Values;
            RouteValueDictionary routeDefaults = this.Route.Defaults;
            // if there is any target type resolved (there was something in action or in defaults - try to retype route.Values into it
            object            rawValue = routeValues[paramName];
            RouteMatchedParam result   = MatchedCompleter._setUpNewMatchedParamResult(paramName, rawValue, targetType);

            if (actionType is Type)
            {
                // pokud je typ parametru na akci definován a má i nemá defaultní hodnotu, tak i když je v url
                // zkonvertovatelná hodnota, nesmyslná hodnota či nic, vždy se použije do hodnoty parametru v akci to,
                // co je definováno za defauktní hodnotu routy - překonvertovateln do typu parametru akce.
                // - pro takové případy kdyby se měla stejně nakonec použít překonvertovatelná hodnota z url a místo toho se použije route.Default- zobrazt WEIRD!
                // - pokud tedy dojde k úspěčné konverzi z hodnoty query stringu - zobrazíme navíc i tuto hodnotu jako variantu
                // - pokud nedojde k dobré konverzi pak taky jako podvariantu ale s jiným komentářem
                // - pokud v query stringu nic není - zobrazíme jen to co je nakonec dosazeno z route default
                // - pokud v route default je třeba nějaký nezkonvertovatelný nesmysl - pak se routa nematchne, nikdy k situaci nedochází
            }
            else
            {
                // pokud není typ parametru v akci definován a metoda je bez něj, zbývá zobrazit buď jen hlášky:
                // - nothing, not contained in action params - pro případ že nic v query stringu ani neni
                //	 - někdy když je query string hodnota zkonvertovatelna do ciloveho typu podle route.Defaults, pak to zobrazit jako variantu
                //	   že je možné použít i toto po manuálním přetypování, pokud to přetypovat nepujde - zobrazit co v query stringu bylo jako raw...
                //   - vždy se do route.Values stejně dosadí to co je v route.Defaults - i tak zobrazit
            }
            return(result);
        }
Beispiel #2
0
        private void _completePredefinedParams(
            List <string> paramsCollection,
            ParamsCompleterHandler paramsCompleterWhenRouteValuesContainsValue,
            ParamsCompleterHandler paramsCompleterWhenRouteValuesContainsNothing
            )
        {
            RouteValueDictionary routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
            Type actionType;
            Type routeDefaultsType;
            Type targetType;

            Type[]            types;
            RouteMatchedParam matchedParamToRender;

            foreach (string paramName in paramsCollection)
            {
                if (paramName == "controller" || paramName == "action" || this._resolvedParams.Contains(paramName))
                {
                    continue;
                }
                // set params has been resolved
                this._resolvedParams.Add(paramName);
                // try to resolve target type
                types             = this._getActionTypeRouteDefaultTypeAndTargetType(paramName);
                actionType        = types[0];
                routeDefaultsType = types[1];
                targetType        = types[2];
                if (routeValues.ContainsKey(paramName))
                {
                    matchedParamToRender = paramsCompleterWhenRouteValuesContainsValue(
                        actionType, routeDefaultsType, targetType, paramName
                        );
                }
                else
                {
                    matchedParamToRender = paramsCompleterWhenRouteValuesContainsNothing(
                        actionType, routeDefaultsType, targetType, paramName
                        );
                }
                this._predefinedParams.Add(
                    MatchedCompleter._renderParamWithVariations(paramName, matchedParamToRender)
                    );
            }
        }
Beispiel #3
0
        private RouteMatchedParam _completeParamsByRouteUrlWhenRouteValuesContainsValue(
            Type actionType, Type routeDefaultsType, Type targetType, string paramName
            )
        {
            RouteValueDictionary routeValues   = HttpContext.Current.Request.RequestContext.RouteData.Values;
            RouteValueDictionary routeDefaults = this.Route.Defaults;
            // if there is any target type resolved (there was something in action or in defaults - try to retype route.Values into it
            object            rawValue = routeValues[paramName];
            RouteMatchedParam result   = MatchedCompleter._setUpNewMatchedParamResult(paramName, rawValue, targetType);

            if (result.PrimaryValue != null)
            {
                // retyping was possible - there could be used value from url successfully retyped or default value successfully retyped
                if (routeDefaultsType is Type)
                {
                    if (
                        routeDefaultsType.FullName == routeValues[paramName].GetType().FullName&&
                        routeDefaults[paramName].GetHashCode() == routeValues[paramName].GetHashCode()
                        )
                    {
                        if (actionType is Type)
                        {
                            result.PrimaryValueDescription = new[] { "default", "from route default" };
                        }
                        else
                        {
                            result.PrimaryValueDescription = new[] { "default", "from route default in route values" };
                        }
                    }
                }
                if (result.PrimaryValueDescription.Length < 2)
                {
                    if (actionType is Type)
                    {
                        result.PrimaryValueDescription = new[] { "ok", "from url" };
                    }
                    else
                    {
                        result.PrimaryValueDescription = new[] { "ok", "in route values" };
                        result.ValueVariations.Add("after manual retype", result.PrimaryValue);
                        result.PrimaryValue = rawValue;
                    }
                }
            }
            else
            {
                // there is some value in url, but not matching exactly target type
                // so there should be used default value from route.Default or default value from action or nothing
                if (actionType is Type && this.Target.Params[paramName].DefaultValue is System.DBNull)
                {
                    // method has argument named as paramName but with no default value - doesn't matter if method argument type is Nullable(?) or not
                    if (this.Target.Params[paramName].IsNullable)
                    {
                        result.PrimaryValue = null;
                        if (routeDefaultsType is Type && routeDefaultsType.FullName == actionType.FullName)
                        {
                            result.PrimaryValueDescription = new[] { "weird", "from action default", "WTF: why it's not used the route default value?" };
                        }
                        else
                        {
                            // some value in url, not matching exactly target type, method has paramName without default, but nullable
                            result.PrimaryValueDescription = new[] { "default", "from action default" };
                        }
                        // display route.Values[paramName] as subvalue if any
                        if (routeValues.ContainsKey(paramName) && rawValue != null)
                        {
                            Type rawValueType = rawValue.GetType();
                            rawValueType = Nullable.GetUnderlyingType(rawValueType) ?? rawValueType;
                            if (rawValueType.Name != "UrlParameter" && rawValueType.FullName != actionType.FullName)
                            {
                                result.ValueVariations.Add("in route values", routeValues[paramName]);
                            }
                            else if (rawValueType.Name != "UrlParameter")
                            {
                                result.ValueVariations.Add("url value", routeValues[paramName]);
                            }
                        }
                    }
                    else
                    {
                        // here is always MVC Exception - so display at least what is in route.Values[paramName]
                        result.PrimaryValue            = routeValues[paramName];
                        result.PrimaryValueDescription = new[] { "weird", "not possible", "add default value to action:-(" };
                    }
                }
                else if (actionType is Type)
                {
                    // method has it's own default argument value
                    if (routeDefaultsType is Type && routeDefaultsType.FullName == actionType.FullName)
                    {
                        result.PrimaryValueDescription = new[] { "weird", "from action default", "WTF: why it's not used the default route value?" };
                    }
                    else
                    {
                        result.PrimaryValueDescription = new[] { "default", "from action default" };
                    }
                    result.PrimaryValue = this.Target.Params[paramName].DefaultValue;
                    // display route.Values[paramName] as subvalue if any
                    if (routeValues.ContainsKey(paramName))
                    {
                        if (rawValue != null)
                        {
                            if (rawValue.GetType().Name != "UrlParameter")
                            {
                                result.ValueVariations.Add("url value", routeValues[paramName]);
                            }
                        }
                    }
                }
                else
                {
                    // method has not any argument with paramName
                    if (routeValues.ContainsKey(paramName))
                    {
                        if (rawValue != null)
                        {
                            if (rawValue.GetType().Name == "UrlParameter")
                            {
                                result.PrimaryValue            = System.DBNull.Value;
                                result.PrimaryValueDescription = new[] { "empty", "nothing, not contained in route values" };
                            }
                        }
                    }
                    if (result.PrimaryValueDescription.Length < 2)
                    {
                        result.PrimaryValue            = routeValues[paramName];
                        result.PrimaryValueDescription = new[] { "raw", "from url in route values" };
                    }
                }
            }
            return(result);
        }