Skip to content

Commit d99fa81

Browse files
authored
Uri F# snippets (#8055)
1 parent 835ae54 commit d99fa81

File tree

27 files changed

+633
-0
lines changed

27 files changed

+633
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="source.fs" />
9+
<Compile Include="source1.fs" />
10+
<Compile Include="source2.fs" />
11+
<Compile Include="nclurienhancements.fs" />
12+
<Compile Include="source3.fs" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
module nclurienhancements
2+
3+
open System
4+
5+
let sampleConstructor () =
6+
//<snippet2>
7+
// Create an absolute Uri from a string.
8+
let absoluteUri = Uri "http://www.contoso.com/"
9+
10+
// Create a relative Uri from a string. allowRelative = true to allow for
11+
// creating a relative Uri.
12+
let relativeUri = Uri("/catalog/shownew.htm?date=today", UriKind.Relative)
13+
14+
// Check whether the new Uri is absolute or relative.
15+
if not relativeUri.IsAbsoluteUri then
16+
printfn $"{relativeUri} is a relative Uri."
17+
18+
// Create a new Uri from an absolute Uri and a relative Uri.
19+
let combinedUri = Uri(absoluteUri, relativeUri)
20+
printfn $"{combinedUri.AbsoluteUri}"
21+
//</snippet2>
22+
23+
// OriginalString
24+
let sampleOriginalString () =
25+
//<snippet3>
26+
// Create a new Uri from a string address.
27+
let uriAddress = Uri "HTTP://www.ConToso.com:80//thick%20and%20thin.htm"
28+
29+
// Write the new Uri to the console and note the difference in the two values.
30+
// ToString() gives the canonical version. OriginalString gives the original
31+
// string that was passed to the constructor.
32+
33+
// The following outputs "http://www.contoso.com//thick and thin.htm".
34+
printfn $"{uriAddress.ToString()}"
35+
36+
// The following outputs "HTTP://www.ConToso.com:80//thick%20and%20thin.htm".
37+
printfn $"{uriAddress.OriginalString}"
38+
//</snippet3>
39+
40+
// DNSSafeHost
41+
let sampleDNSSafeHost () =
42+
//<snippet4>
43+
// Create new Uri using a string address.
44+
let address = Uri "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"
45+
46+
// Make the address DNS safe.
47+
48+
// The following outputs "[fe80::200:39ff:fe36:1a2d]".
49+
printfn $"{address.Host}"
50+
51+
// The following outputs "fe80::200:39ff:fe36:1a2d%254".
52+
printfn $"{address.DnsSafeHost}"
53+
//</snippet4>
54+
55+
// operator = and <>
56+
let sampleOperatorEqual () =
57+
//<snippet5>
58+
// Create some Uris.
59+
let address1 = Uri "http://www.contoso.com/index.htm#search"
60+
let address2 = Uri "http://www.contoso.com/index.htm"
61+
let address3 = Uri "http://www.contoso.com/index.htm?date=today"
62+
63+
// The first two are equal because the fragment is ignored.
64+
if address1 = address2 then
65+
printfn $"{address1} is equal to {address2}"
66+
67+
// The second two are not equal.
68+
if address2 <> address3 then
69+
printfn $"{address2} is not equal to {address3}"
70+
//</snippet5>
71+
72+
// IsBaseOf
73+
let sampleIsBaseOf () =
74+
//<snippet6>
75+
// Create a base Uri.
76+
let baseUri = Uri "http://www.contoso.com/"
77+
78+
// Create a new Uri from a string.
79+
let uriAddress = Uri "http://www.contoso.com/index.htm?date=today"
80+
81+
// Determine whether BaseUri is a base of UriAddress.
82+
if baseUri.IsBaseOf uriAddress then
83+
printfn $"{baseUri} is the base of {uriAddress}"
84+
//</snippet6>
85+
86+
// Constructor
87+
sampleConstructor ()
88+
89+
// OriginalString
90+
sampleOriginalString ()
91+
92+
// DNSSafeHost
93+
sampleDNSSafeHost ()
94+
95+
// operator = and <>
96+
sampleOperatorEqual ()
97+
98+
// IsBaseOf
99+
sampleIsBaseOf ()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module source
2+
3+
open System
4+
5+
// <Snippet1>
6+
let myUri = Uri "http://www.contoso.com/"
7+
// </Snippet1>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module source1
2+
3+
open System
4+
5+
// <Snippet1>
6+
let myUri = Uri("http://www.contoso.com/Hello%20World.htm", true)
7+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module source2
2+
3+
open System
4+
5+
// <Snippet1>
6+
let baseUri = Uri "http://www.contoso.com"
7+
let myUri = Uri(baseUri, "catalog/shownew.htm")
8+
9+
printfn $"{myUri}"
10+
// </Snippet1>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module source3
2+
3+
open System
4+
5+
// <Snippet1>
6+
let baseUri = Uri "http://www.contoso.com"
7+
let myUri = Uri(baseUri, "Hello%20World.htm", false)
8+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// <Snippet1>
2+
open System
3+
4+
let baseUri = Uri "http://www.contoso.com/"
5+
let myUri = Uri(baseUri, "catalog/shownew.htm?date=today")
6+
7+
printfn $"{myUri.AbsolutePath}"
8+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// <Snippet1>
2+
open System
3+
4+
let baseUri= Uri "http://www.contoso.com"
5+
let myUri = Uri(baseUri,"catalog/shownew.htm?date=today")
6+
7+
printfn $"{myUri.AbsoluteUri}"
8+
// </Snippet1>

0 commit comments

Comments
 (0)