public HalLink ResolveFor(object Dto, IHalModelConfig config, JsonSerializer serializer, object memberValue = null) { var clone = MemberwiseClone() as HalLink; var dtoType = Dto?.GetType(); UriTemplate template; if (string.IsNullOrEmpty(Href)) { template = new UriTemplate(memberValue as string, true, false); } else { template = new UriTemplate(Href, true, false); foreach (string param in template.GetParameterNames()) { object value; if (param == "value") value = memberValue; else value = dtoType?.GetProperty(param)?.GetValue(Dto); if (value != null) { if (value is string == false) { var token = JToken.FromObject(value, serializer); value = token.Type == JTokenType.String ? token.Value<string>() : value; } template.SetParameter(param, value); } } } var linkUri = new Uri(template.Resolve(), UriKind.RelativeOrAbsolute); if (!linkUri.IsAbsoluteUri) { var baseUri = new Uri(config.RequestPathBase); string basePath = baseUri.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute(config.RelativePathBase, baseUri.AbsolutePath); linkUri = new Uri(new Uri(basePath), linkUri); } clone.Href = linkUri.ToString(); return clone; }
public string AsUriStr() { if (_path != null) { return _path; } if (_resource._uriTemplateStr != null) { var template = new UriTemplate(_resource._uriTemplateStr); var parameterNames = template.GetParameterNames(); var parameterNameList = parameterNames as IList<string> ?? parameterNames.ToList(); if (_pathElements != null) { if (parameterNameList.Count() != _pathElements.Count()) { throw new ArgumentException(String.Format("Mismatch between parameters in uriTemplate and supplied elements; uriTemplate={0}, elements={1}", template.Template, _pathElements)); } int i = 0; foreach (var parameterName in parameterNameList) { template.SetParameter(parameterName, _pathElements[i++]); } } else { var pathElementMapKeys = _pathElementMap.Keys; var argsWithNoParams = pathElementMapKeys.Except(parameterNameList); var paramsWithNoArgs = parameterNameList.Except(pathElementMapKeys); if (argsWithNoParams.Any() || paramsWithNoArgs.Any()) { throw new ArgumentException(String.Format("Mismatch between parameters in uriTemplate and supplied map; uriTemlpate={0}, map={1}", template.Template, _pathElementMap)); } foreach (var parameterName in parameterNameList) { template.SetParameter(parameterName, _pathElementMap[parameterName]); } } return PrefixBaseIfRequired(template.Resolve()); } // else return PrefixBaseIfRequired(_resource._uriStr); }
/// <summary> /// Resolves the URI Template defined in the Target URI using the assigned URI parameters /// </summary> /// <returns></returns> public Uri GetResolvedTarget() { if (Target != null ) { var uriTemplate = new UriTemplate(Target.OriginalString); if (AddNonTemplatedParametersToQueryString) { var templateParameters = uriTemplate.GetParameterNames(); if (_Parameters.Any(p => !templateParameters.Contains(p.Key))) { AddParametersAsTemplate(); uriTemplate = new UriTemplate(Target.OriginalString); } } if (Target.OriginalString.Contains("{")) { ApplyParameters(uriTemplate); var resolvedTarget = new Uri(uriTemplate.Resolve(), UriKind.RelativeOrAbsolute); return resolvedTarget; } } return Target; }
/// <summary> /// Returns list of URI Template parameters specified in the Target URI /// </summary> /// <returns></returns> public IEnumerable<string> GetParameterNames() { var uriTemplate = new UriTemplate(Target.OriginalString); return uriTemplate.GetParameterNames(); }