This repository was archived by the owner on May 20, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
add csharp reference docs #712
Closed
HomelessDinosaur
wants to merge
14
commits into
nitrictech:main
from
HomelessDinosaur:csharp-reference
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dfb67a6
add csharp reference docs
HomelessDinosaur fb656c3
Apply suggestions from code review
raksiv 1ff4890
Add index/reference to menu
raksiv 3917bda
Add return statement to examples
raksiv 6c39bb2
fix references to middleware
HomelessDinosaur 7a51178
fix ApiOptions import
raksiv 7640d76
Fix API reference examples for middleware, basepath and security.
raksiv fa19cac
Fix API parameters to match SDK
raksiv 404e3ec
Fix dequeue examples
raksiv 623cd64
fix secret examples that cause warnings for uppercase name
raksiv 7695199
fix secrets value access example
raksiv 53e82c9
fix string literal issue with bucket example
raksiv 737f563
add sdk docs back links
raksiv e251611
change to use async handlers and format
HomelessDinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| --- | ||
| description: "Reference for Nitric's .NET library - Register an API route and set a specific HTTP DELETE handler on that route." | ||
| --- | ||
|
|
||
| # .NET - Api.Delete() | ||
|
|
||
| <Note> | ||
| This is reference documentation for the Nitric .NET SDK. To learn about APIs | ||
| in Nitric start with the [API docs](/api). | ||
| </Note> | ||
|
|
||
| Register an API route and set a specific HTTP DELETE handler on that route. | ||
|
|
||
| <Note> | ||
| This method is a convenient short version of | ||
| [Api().Route().Delete()](./api-route-delete) | ||
| </Note> | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Delete("/hello/:name", async (ctx) => { | ||
| var name = ctx.Req.PathParams["name"]; | ||
|
|
||
| ctx.Res.Text($"Deleting {name}!"); | ||
|
|
||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| <Properties> | ||
| <Property name="match" type="string" required> | ||
| The path matcher to use for the route. Matchers accept path parameters in | ||
| the form of a colon prefixed string. The string provided will be used as | ||
| that path parameter's name when calling middleware and handlers. See [create | ||
| a route with path params](#create-a-route-with-path-params) | ||
| </Property> | ||
| <Property | ||
| name="...middleware" | ||
| type="Middleware<HttpContext> or Func<HttpContext, HttpContext>" | ||
| required | ||
| > | ||
| One or more middleware functions to use as the handler for HTTP requests. | ||
| Handlers can be sync or async. | ||
| </Property> | ||
| </Properties> | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Register a handler for DELETE requests | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Delete("/hello/:name", async (ctx) => { | ||
| var name = ctx.Req.PathParams["name"]; | ||
|
|
||
| ctx.Res.Text($"Deleting {name}!"); | ||
|
|
||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ### Chain functions as a single method handler | ||
|
|
||
| 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. | ||
|
|
||
| ```csharp | ||
| using Nitric.Sdk.Service; | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Delete("/hello/:userId", | ||
| new Middleware<HttpContext>[] { | ||
| async (ctx, next) => { | ||
| var user = ctx.Req.PathParams["userId"]; | ||
|
|
||
| // Validate the user identity | ||
| if (user != "1234") | ||
| { | ||
| ctx.Res.Text($"User {user} is unauthorised"); | ||
| ctx.Res.Status = 403; | ||
|
|
||
| // Return prematurely to end the middleware chain. | ||
| return ctx; | ||
| } | ||
|
|
||
| // Call next to continue the middleware chain. | ||
| return await next(ctx); | ||
| }, async (ctx, next) => { | ||
| var user = ctx.Req.PathParams["userId"]; | ||
|
|
||
| ctx.Res.Text($"Deleting {user}"); | ||
|
|
||
| return await next(ctx); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ### Access the request body | ||
|
|
||
| The DELETE request body is accessible from the `context.Req` object. | ||
|
|
||
| ```csharp | ||
| using System.Collections.Generic; | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Delete("/hello/:name", async (ctx) => { | ||
| var body = ctx.Req.Json<Dictionary<string, string>>(); | ||
| // parse, validate and store the request payload... | ||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| --- | ||
| description: "Reference for Nitric's .NET library - Register an API route and set a specific HTTP GET handler on that route." | ||
| --- | ||
|
|
||
| # .NET - Api.Get() | ||
|
|
||
| <Note> | ||
| This is reference documentation for the Nitric .NET SDK. To learn about APIs | ||
| in Nitric start with the [API docs](/api). | ||
| </Note> | ||
|
|
||
| Register an API route and set a specific HTTP GET handler on that route. | ||
|
|
||
| <Note> | ||
| This method is a convenient short version of | ||
| [Api().Route().Get()](./api-route-get) | ||
| </Note> | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Get("/hello/:name", async (ctx) => { | ||
| var name = ctx.Req.PathParams["name"]; | ||
|
|
||
| ctx.Res.Text($"Getting {name}!"); | ||
|
|
||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| <Properties> | ||
| <Property name="match" type="string" required> | ||
| The path matcher to use for the route. Matchers accept path parameters in | ||
| the form of a colon prefixed string. The string provided will be used as | ||
| that path parameter's name when calling middleware and handlers. See [create | ||
| a route with path params](#create-a-route-with-path-params) | ||
| </Property> | ||
| <Property | ||
| name="...middleware" | ||
| type="Middleware<HttpContext> or Func<HttpContext, HttpContext>" | ||
| required | ||
| > | ||
| One or more middleware functions to use as the handler for HTTP requests. | ||
| Handlers can be sync or async. | ||
| </Property> | ||
| </Properties> | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Register a handler for GET requests | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Get("/hello/:name", async (ctx) => { | ||
| var name = ctx.Req.PathParams["name"]; | ||
|
|
||
| ctx.Res.Text($"Getting {name}!"); | ||
|
|
||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ### Chain functions as a single method handler | ||
|
|
||
| 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. | ||
|
|
||
| ```csharp | ||
| using Nitric.Sdk.Service; | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Get("/hello/:userId", | ||
| new Middleware<HttpContext>[] { | ||
| async (ctx, next) => { | ||
| var user = ctx.Req.PathParams["userId"]; | ||
|
|
||
| // Validate the user identity | ||
| if (user != "1234") | ||
| { | ||
| ctx.Res.Text($"User {user} is unauthorised"); | ||
| ctx.Res.Status = 403; | ||
|
|
||
| // Return prematurely to end the middleware chain. | ||
| return ctx; | ||
| } | ||
|
|
||
| // Call next to continue the middleware chain. | ||
| return await next(ctx); | ||
| }, async (ctx, next) => { | ||
| var user = ctx.Req.PathParams["userId"]; | ||
|
|
||
| ctx.Res.Text($"Getting {user}"); | ||
|
|
||
| return await next(ctx); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| Application.Run(); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| --- | ||
| description: "Reference for Nitric's .NET library - Register an API route and set a specific HTTP PATCH handler on that route." | ||
| --- | ||
|
|
||
| # .NET - Api.Patch() | ||
|
|
||
| <Note> | ||
| This is reference documentation for the Nitric .NET SDK. To learn about APIs | ||
| in Nitric start with the [API docs](/api). | ||
| </Note> | ||
|
|
||
| Register an API route and set a specific HTTP PATCH handler on that route. | ||
|
|
||
| <Note> | ||
| This method is a convenient short version of | ||
| [Api().Route().Patch()](./api-route-patch) | ||
| </Note> | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Patch("/hello/:name", async (ctx) => { | ||
| var name = ctx.Req.PathParams["name"]; | ||
|
|
||
| ctx.Res.Text($"Patching {name}!"); | ||
|
|
||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| <Properties> | ||
| <Property name="match" type="string" required> | ||
| The path matcher to use for the route. Matchers accept path parameters in | ||
| the form of a colon prefixed string. The string provided will be used as | ||
| that path parameter's name when calling middleware and handlers. See [create | ||
| a route with path params](#create-a-route-with-path-params) | ||
| </Property> | ||
| <Property | ||
| name="...middleware" | ||
| type="Middleware<HttpContext> or Func<HttpContext, HttpContext>" | ||
| required | ||
| > | ||
| One or more middleware functions to use as the handler for HTTP requests. | ||
| Handlers can be sync or async. | ||
| </Property> | ||
| </Properties> | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Register a handler for PATCH requests | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Patch("/hello/:name", async (ctx) => { | ||
| var name = ctx.Req.PathParams["name"]; | ||
|
|
||
| ctx.Res.Text($"Patching {name}!"); | ||
|
|
||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ### Chain functions as a single method handler | ||
|
|
||
| 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. | ||
|
|
||
| ```csharp | ||
| using Nitric.Sdk.Service; | ||
| using Application = Nitric.Sdk.Nitric; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Patch("/hello/:userId", | ||
| new Middleware<HttpContext>[] { | ||
| async (ctx, next) => { | ||
| var user = ctx.Req.PathParams["userId"]; | ||
|
|
||
| // Validate the user identity | ||
| if (user != "1234") | ||
| { | ||
| ctx.Res.Text($"User {user} is unauthorised"); | ||
| ctx.Res.Status = 403; | ||
|
|
||
| // Return prematurely to end the middleware chain. | ||
| return ctx; | ||
| } | ||
|
|
||
| // Call next to continue the middleware chain. | ||
| return await next(ctx); | ||
| }, async (ctx, next) => { | ||
| var user = ctx.Req.PathParams["userId"]; | ||
|
|
||
| ctx.Res.Text($"Patching {user}"); | ||
|
|
||
| return await next(ctx); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
|
|
||
| ### Access the request body | ||
|
|
||
| The PATCH request body is accessible from the `context.Req` object. | ||
|
|
||
| ```csharp | ||
| using Application = Nitric.Sdk.Nitric; | ||
raksiv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Collections.Generic; | ||
|
|
||
| var api = Application.Api("main"); | ||
|
|
||
| api.Patch("/hello/:name", async (ctx) => { | ||
| var body = ctx.Req.Json<Dictionary<string, string>>(); | ||
| // parse, validate and store the request payload... | ||
| return ctx; | ||
| }); | ||
|
|
||
| Application.Run(); | ||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.