public bool TryGetValue(RelativeRoute routePart, out TValue value)
        {
            var stringValue = routePart.PathSegments.Single();

            if (string.IsNullOrEmpty(stringValue))
            {
                value = default(TValue);
                return(false);
            }

            if (IsGuidField)
            {
                Guid tempGuid;

                if (!UrlUtils.TryExpandGuid(stringValue, out tempGuid) && !Guid.TryParse(stringValue, out tempGuid))
                {
                    value = default(TValue);
                    return(false);
                }

                value = (TValue)(tempGuid as object);
                return(true);
            }

            if (IsStringField)
            {
                value = (TValue)(UrlUtils.DecodeUrlInvalidCharacters(stringValue) as object);
                return(true);
            }

            Exception exception;
            object    valueObj = ValueTypeConverter.TryConvert(stringValue, typeof(TValue), out exception);

            bool success = valueObj != null;

            value = success ? (TValue)valueObj : default(TValue);

            return(success);
        }
예제 #2
0
        public RoutedDataModel GetRouteDataModel(PageUrlData pageUrlData)
        {
            string pathInfo = pageUrlData.PathInfo;

            if (pathInfo.IsNullOrEmpty())
            {
                return(new RoutedDataModel(GetListQueryable));
            }

            switch (_dataRouteKind)
            {
            case DataRouteKind.Key:
            case DataRouteKind.KeyAndLabel:
            {
                string key;
                string label = null;

                if (_dataRouteKind == DataRouteKind.KeyAndLabel)
                {
                    string[] parts = pathInfo.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length > 2)
                    {
                        return(new RoutedDataModel());
                    }
                    key   = parts[0];
                    label = parts.Length == 2 ? parts[1] : null;
                }
                else
                {
                    if (pathInfo.Length < 2 || pathInfo.LastIndexOf('/') > 0)
                    {
                        return(new RoutedDataModel());
                    }

                    key = pathInfo.Substring(1);
                }

                var    keyType  = _keyPropertyInfo.PropertyType;
                object keyValue = ValueTypeConverter.Convert(key, keyType);

                if (keyValue == null ||
                    (keyValue is Guid && (Guid)keyValue == Guid.Empty && key != Guid.Empty.ToString()))
                {
                    return(new RoutedDataModel());
                }

                var data = DataFacade.TryGetDataByUniqueKey <T>(keyValue);

                return(new RoutedDataModel(data));
            }

            case DataRouteKind.Label:
            {
                if (pathInfo.Length < 2 || pathInfo.LastIndexOf('/') > 0)
                {
                    return(new RoutedDataModel());
                }

                string label = UrlUtils.DecodeUrlInvalidCharacters(pathInfo.Substring(1));

                var data = GetDataByLabel(label);
                return(new RoutedDataModel(data));
            }

            default:
                throw new InvalidOperationException("Not supported data url kind: " + _dataRouteKind);
            }
        }