> For the complete documentation index, see [llms.txt](https://docs.devolutions.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devolutions.net/powershell-universal/da/platform/plugins/c-api-endpoints.md).

# C#-API-slutpunkter

Byg hurtigere API-slutpunkter i PowerShell Universal ved hjælp af pluginet C Sharp API Endpoints med support til brugerdefinerede assemblies og hemmelighedsvariabler.

**Identifikator:** `PowerShellUniversal.Language.CSharp`

Dette plugin opretter et C#-baseret miljø, der kan bruges til at oprette API-slutpunkter med C#-kode. API'er, der er oprettet med C#, er meget hurtigere end PowerShell-baserede slutpunkter. Slutpunkter kører direkte i PowerShell Universal-tjenesten. Enhver undtagelse, der udløses fra Deres slutpunkt, håndteres, og en gyldig statuskode returneres til kalderen.

De skal oprette slutpunkter med parameteren -Path og angive miljøet `C#`, for at slutpunktet fungerer korrekt.

```powershell
New-PSUEndpoint -Url /csharp -Path csharp.cs -Environment 'C#'
```

**Definition af et slutpunkt**

I C#-slutpunktet er der to klasser, der er af interesse. Den første er variablen `request`, der sendes til slutpunktet. Det er et `ApiRequest`-objekt.

```csharp
public class ApiRequest
{
    public long Id { get; set; }
    public ICollection<KeyValue> Variables { get; set; } = new List<KeyValue>();
    public IEnumerable<ApiFile> Files { get; set; } = new List<ApiFile>();
    public string Url { get; set; }
    public ICollection<KeyValue> Headers { get; set; } = new List<KeyValue>();
    public byte[] Data { get; set; }
    public int ErrorAction { get; set; }
    public ICollection<KeyValue> Parameters { get; set; } = new List<KeyValue>();
    public string Method { get; set; }
    public ICollection<KeyValue> Cookies { get; set; } = new List<KeyValue>();
    public string ClaimsPrincipal { get; set; }
    public string ContentType { get; set; }
    public string[] Roles { get; set; }
}
```

I Deres slutpunkt kan De få adgang til denne variabel automatisk.

```csharp
if (request.ContentType == "application/json")
{
     // Do some stuff with JSON
}
```

Slutpunktet skal returnere et `ApiResponse`-objekt. Dette objekt har følgende definition.

```csharp
public class ApiResponse
{
    public int StatusCode { get; set; } = 200;
    public string Body { get; set; }
    public List<KeyValue> Cookies { get; set; } = new List<KeyValue>();
    public byte[] Data { get; set; } = Array.Empty<byte>();
    public string ContentType { get; set; } = "text/plain";
    public List<KeyValue> Headers { get; set; } = new List<KeyValue>();
    public ApiFile File { get; set; }
}
```

De kan returnere et svar ved at oprette et nyt objekt og returnere det fra Deres slutpunkt.

```csharp
return new ApiResponse {
    StatusCode = 401
};
```

Når De definerer et slutpunkts indhold, skal De forstå, at koden føjes til en dynamisk navngivet klasse med en enkelt Execute-funktion. Deres kode skal være gyldig syntaks for en sådan kildefil.

```csharp
using PowerShellUniversal;

public class c{id} : ExecutionClass {{ 
    public static ApiResponse Execute(ApiRequest request) 
    {{ 
        {fileContents} 

        return new ApiResponse();
    }} 
}}";
```

De kan få adgang til PowerShell Universal-tjenestecontaineren i Deres slutpunkt ved at få adgang til egenskaben `ServiceProvider` i Deres slutpunkt. Vi dokumenterer i øjeblikket ikke de interne tjenester i PowerShell Universal.

```csharp
var database = ServiceProvider.GetService(typeof(IDatabase));
```

### Referencer

De kan styre, hvilke assemblies der refereres til, ved hjælp af nøgleordet `#ref`. Værdien kan være en DLL-fil i installationskataloget for PowerShell Universal, eller den fulde sti til en anden assembly.

```csharp
#ref PowerShellUniversal.Apis.dll
#ref C:\assemblies\markdiag.dll
```

### Brug af navneområde

De kan referere til et navneområde ved hjælp af nøgleordet `#using`.

```csharp
#using System.Management.Automation
```

### Variabler

De kan få adgang til variabler i C#-slutpunkter med metoderne `GetVariable`, `GetSecretString` og `GetSecretCredential`.

```csharp
return new ApiResponse {
    StatusCode = 200
    Body = GetVariable("MyVar").ToString()
};
```

For at få adgang til en `PSCredential` kan De gøre følgende.

```csharp
#ref System  
#ref System.Management.Automation

return new ApiResponse { 
    StatusCode = 200, 
    Body = GetSecretCredential("MyCred").UserName.ToString() 
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.devolutions.net/powershell-universal/da/platform/plugins/c-api-endpoints.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
