Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 1ad5a73

Browse files
add csharp reference docs
1 parent 6c5cfe6 commit 1ad5a73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4007
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
description: "Reference for Nitric's .NET library - Register an API route and set a specific HTTP DELETE handler on that route."
3+
---
4+
5+
# .NET - Api.Delete()
6+
7+
Register an API route and set a specific HTTP DELETE handler on that route.
8+
9+
<Note>
10+
This method is a convenient short version of
11+
[Api().Route().Delete()](./api-route-delete)
12+
</Note>
13+
14+
```csharp
15+
using Application = Nitric.Sdk.Nitric;
16+
17+
var api = Application.Api("main");
18+
19+
api.Delete("/hello/:name", context => {
20+
var name = context.Req.PathParams.get("name");
21+
22+
context.Res.Text($"Deleting {name}!");
23+
24+
return context;
25+
});
26+
27+
Application.Run();
28+
```
29+
30+
## Parameters
31+
32+
<Properties>
33+
<Property name="match" type="string" required>
34+
The path matcher to use for the route. Matchers accept path parameters in
35+
the form of a colon prefixed string. The string provided will be used as
36+
that path parameter's name when calling middleware and handlers. See [create
37+
a route with path params](#create-a-route-with-path-params)
38+
</Property>
39+
<Property
40+
name="...middleware"
41+
type="Middleware&lt;HttpContext&gt; or Func&lt;HttpContext, HttpContext&gt;"
42+
required
43+
>
44+
One or more middleware functions to use as the handler for HTTP requests.
45+
Handlers can be sync or async.
46+
</Property>
47+
</Properties>
48+
49+
## Examples
50+
51+
### Register a handler for DELETE requests
52+
53+
```csharp
54+
using Application = Nitric.Sdk.Nitric;
55+
56+
var api = Application.Api("main");
57+
58+
api.Delete("/hello/:name", context => {
59+
var name = context.Req.PathParams.get("name");
60+
61+
context.Res.Text($"Deleting {name}!");
62+
63+
return context;
64+
});
65+
66+
Application.Run();
67+
```
68+
69+
### Chain functions as a single method handler
70+
71+
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
72+
73+
```csharp
74+
using Nitric.Sdk;
75+
76+
var api = Nitric.Api("main");
77+
78+
api.Delete("/hello/:userId",
79+
(context, next) => {
80+
var user = context.Req.PathParams["userId"];
81+
82+
// Validate the user identity
83+
if (user != "1234")
84+
{
85+
context.Res.Text($"User {user} is unauthorised");
86+
context.Res.Status = 403;
87+
88+
// Return prematurely to end the middleware chain.
89+
return context;
90+
}
91+
92+
// Call next to continue the middleware chain.
93+
return next(context);
94+
}, (context, next) => {
95+
var user = context.Req.PathParams["userId"];
96+
97+
context.Res.Text($"Deleting {user}");
98+
99+
return next(context);
100+
}
101+
);
102+
103+
Nitric.Run();
104+
```
105+
106+
### Access the request body
107+
108+
The DELETE request body is accessible from the `context.Req` object.
109+
110+
```csharp
111+
using Nitric.Sdk;
112+
113+
var api = Nitric.Api("main");
114+
115+
api.Delete("/hello/:name", context => {
116+
var body = context.Req.Json<Dictionary<string, string>>();
117+
// parse, validate and store the request payload...
118+
});
119+
120+
Nitric.Run();
121+
```
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
description: "Reference for Nitric's .NET library - Register an API route and set a specific HTTP GET handler on that route."
3+
---
4+
5+
# .NET - Api.Get()
6+
7+
Register an API route and set a specific HTTP GET handler on that route.
8+
9+
<Note>
10+
This method is a convenient short version of
11+
[Api().Route().Get()](./api-route-get)
12+
</Note>
13+
14+
```csharp
15+
using Application = Nitric.Sdk.Nitric;
16+
17+
var api = Application.Api("main");
18+
19+
api.Get("/hello/:name", context => {
20+
var name = context.Req.PathParams.get("name");
21+
22+
context.Res.Text($"Getting {name}!");
23+
24+
return context;
25+
});
26+
27+
Application.Run();
28+
```
29+
30+
## Parameters
31+
32+
<Properties>
33+
<Property name="match" type="string" required>
34+
The path matcher to use for the route. Matchers accept path parameters in
35+
the form of a colon prefixed string. The string provided will be used as
36+
that path parameter's name when calling middleware and handlers. See [create
37+
a route with path params](#create-a-route-with-path-params)
38+
</Property>
39+
<Property
40+
name="...middleware"
41+
type="Middleware&lt;HttpContext&gt; or Func&lt;HttpContext, HttpContext&gt;"
42+
required
43+
>
44+
One or more middleware functions to use as the handler for HTTP requests.
45+
Handlers can be sync or async.
46+
</Property>
47+
</Properties>
48+
49+
## Examples
50+
51+
### Register a handler for GET requests
52+
53+
```csharp
54+
using Application = Nitric.Sdk.Nitric;
55+
56+
var api = Application.Api("main");
57+
58+
api.Get("/hello/:name", context => {
59+
var name = context.Req.PathParams.get("name");
60+
61+
context.Res.Text($"Getting {name}!");
62+
63+
return context;
64+
});
65+
66+
Application.Run();
67+
```
68+
69+
### Chain functions as a single method handler
70+
71+
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
72+
73+
```csharp
74+
using Application = Nitric.Sdk.Nitric;
75+
76+
var api = Application.Api("main");
77+
78+
api.Get("/hello/:userId",
79+
(context, next) => {
80+
var user = context.Req.PathParams["userId"];
81+
82+
// Validate the user identity
83+
if (user != "1234")
84+
{
85+
context.Res.Text($"User {user} is unauthorised");
86+
context.Res.Status = 403;
87+
88+
// Return prematurely to end the middleware chain.
89+
return context;
90+
}
91+
92+
// Call next to continue the middleware chain.
93+
return next(context);
94+
}, (context, next) => {
95+
var user = context.Req.PathParams["userId"];
96+
97+
context.Res.Text($"Getting {user}");
98+
99+
return next(context);
100+
}
101+
);
102+
103+
Application.Run();
104+
```
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
description: "Reference for Nitric's .NET library - Register an API route and set a specific HTTP PATCH handler on that route."
3+
---
4+
5+
# .NET - Api.Patch()
6+
7+
Register an API route and set a specific HTTP PATCH handler on that route.
8+
9+
<Note>
10+
This method is a convenient short version of
11+
[Api().Route().Patch()](./api-route-patch)
12+
</Note>
13+
14+
```csharp
15+
using Application = Nitric.Sdk.Nitric;
16+
17+
var api = Application.Api("main");
18+
19+
api.Patch("/hello/:name", context => {
20+
var name = context.Req.PathParams.get("name");
21+
22+
context.Res.Text($"Patching {name}!");
23+
24+
return context;
25+
});
26+
27+
Application.Run();
28+
```
29+
30+
## Parameters
31+
32+
<Properties>
33+
<Property name="match" type="string" required>
34+
The path matcher to use for the route. Matchers accept path parameters in
35+
the form of a colon prefixed string. The string provided will be used as
36+
that path parameter's name when calling middleware and handlers. See [create
37+
a route with path params](#create-a-route-with-path-params)
38+
</Property>
39+
<Property
40+
name="...middleware"
41+
type="Middleware&lt;HttpContext&gt; or Func&lt;HttpContext, HttpContext&gt;"
42+
required
43+
>
44+
One or more middleware functions to use as the handler for HTTP requests.
45+
Handlers can be sync or async.
46+
</Property>
47+
</Properties>
48+
49+
## Examples
50+
51+
### Register a handler for PATCH requests
52+
53+
```csharp
54+
using Application = Nitric.Sdk.Nitric;
55+
56+
var api = Application.Api("main");
57+
58+
api.Patch("/hello/:name", context => {
59+
var name = context.Req.PathParams.get("name");
60+
61+
context.Res.Text($"Patching {name}!");
62+
63+
return context;
64+
});
65+
66+
Application.Run();
67+
```
68+
69+
### Chain functions as a single method handler
70+
71+
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
72+
73+
```csharp
74+
using Application = Nitric.Sdk.Nitric;
75+
76+
var api = Application.Api("main");
77+
78+
api.Patch("/hello/:userId",
79+
(context, next) => {
80+
var user = context.Req.PathParams["userId"];
81+
82+
// Validate the user identity
83+
if (user != "1234")
84+
{
85+
context.Res.Text($"User {user} is unauthorised");
86+
context.Res.Status = 403;
87+
88+
// Return prematurely to end the middleware chain.
89+
return context;
90+
}
91+
92+
// Call next to continue the middleware chain.
93+
return next(context);
94+
}, (context, next) => {
95+
var user = context.Req.PathParams["userId"];
96+
97+
context.Res.Text($"Patching {user}");
98+
99+
return next(context);
100+
}
101+
);
102+
103+
Application.Run();
104+
```
105+
106+
### Access the request body
107+
108+
The PATCH request body is accessible from the `context.Req` object.
109+
110+
```csharp
111+
using Application = Nitric.Sdk.Nitric;
112+
113+
var api = Application.Api("main");
114+
115+
api.Patch("/hello/:name", context => {
116+
var body = context.Req.Json<Dictionary<string, string>>();
117+
// parse, validate and store the request payload...
118+
});
119+
120+
Application.Run();
121+
```

0 commit comments

Comments
 (0)