> 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/de/plattform/plugins/c-api-endpoints.md).

# C#-API-Endpunkte

**Identifier:** `PowerShellUniversal.Language.CSharp`

Dieses Plugin erstellt eine C#-basierte Umgebung, mit der API-Endpunkte mit C#-Code erstellt werden können. Mit C# erstellte APIs sind viel schneller als PowerShell-basierte Endpunkte. Endpunkte werden direkt im PowerShell Universal-Dienst ausgeführt. Jede von Ihrem Endpunkt ausgelöste Ausnahme wird behandelt und ein gültiger Statuscode wird an den Aufrufer zurückgegeben.

Sie müssen Endpunkte mit dem Parameter -Path erstellen und die `C#`-Umgebung angeben, damit der Endpunkt ordnungsgemäß funktioniert.

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

**Einen Endpunkt definieren**

Innerhalb des C#-Endpunkts sind zwei Klassen von Interesse. Die erste ist die Variable `request`, die an den Endpunkt übergeben wird. Es handelt sich um ein `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; }
}
```

In Ihrem Endpunkt können Sie automatisch auf diese Variable zugreifen.

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

Der Endpunkt muss ein `ApiResponse`-Objekt zurückgeben. Dieses Objekt hat die folgende 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; }
}
```

Sie können eine Antwort zurückgeben, indem Sie ein neues Objekt erstellen und es von Ihrem Endpunkt zurückgeben.

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

Beachten Sie beim Definieren des Inhalts eines Endpunkts, dass der Code einer dynamisch benannten Klasse mit einer einzelnen Execute-Funktion hinzugefügt wird. Ihr Code sollte eine für eine solche Quelldatei gültige Syntax aufweisen.

```csharp
using PowerShellUniversal;

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

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

Sie können auf den PowerShell Universal-Dienstcontainer innerhalb Ihres Endpunkts zugreifen, indem Sie in Ihrem Endpunkt auf die Eigenschaft `ServiceProvider` zugreifen. Wir dokumentieren derzeit nicht die internen Dienste von PowerShell Universal.

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

### Referenzen

Sie können mit dem Schlüsselwort `#ref` steuern, welche Assemblys referenziert werden. Der Wert kann eine DLL-Datei im PowerShell Universal-Installationsverzeichnis oder der vollständige Pfad zu einer anderen Assembly sein.

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

### Namespace Using

Sie können einen Namespace mit dem Schlüsselwort `#using` referenzieren.

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

### Variablen

Sie können in C#-Endpunkten mit den Methoden `GetVariable`, `GetSecretString` und `GetSecretCredential` auf Variablen zugreifen.

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

Um auf ein `PSCredential` zuzugreifen, können Sie Folgendes tun.

```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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
