/
Link.cs
63 lines (54 loc) · 2.25 KB
/
Link.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Text.RegularExpressions;
using System.Web;
namespace WebApi.Hal
{
public class Link
{
public Link()
{ }
public Link(string rel, string href)
{
Rel = rel;
Href = href;
if (href != null)
IsTemplated = Regex.Match(href, @"{\w+}", RegexOptions.Compiled).Success;
}
public string Rel { get; set; }
public string Href { get; set; }
public bool IsTemplated { get; set; }
/// <summary>
/// If this link is templated, you can use this method to make a non templated copy
/// </summary>
/// <param name="newRel">A different rel</param>
/// <param name="substitutions">Pass in substitutions like 'name=>"Value"',
/// if you have a conflicting variable like page, you can do '_page => page + 1'</param>
/// <returns>A non templated link</returns>
public Link CreateLink(string newRel, params Func<string, object>[] substitutions)
{
return new Link(newRel, CreateUri(substitutions).ToString());
}
/// <summary>
/// If this link is templated, you can use this method to make a non templated copy
/// </summary>
/// <param name="substitutions">Pass in substitutions like 'name=>"Value"',
/// if you have a conflicting variable like page, you can do '_page => page + 1'</param>
/// <returns>A non templated link</returns>
public Link CreateLink(params Func<string, object>[] substitutions)
{
return CreateLink(Rel, substitutions);
}
public Uri CreateUri(params Func<string, object>[] substitutions)
{
var href = Href;
foreach (var substitution in substitutions)
{
var name = substitution.Method.GetParameters()[0].Name.Trim('_');
var value = substitution(null);
var substituionValue = value == null ? null : HttpUtility.UrlEncode(value.ToString());
href = href.Replace(string.Format("{{{0}}}", name), substituionValue);
}
return new Uri(href, UriKind.Relative);
}
}
}