Ich schreibe einen kleinen Aktionsfilter für ein ASP.NET Core-Web-API-Projekt. Der Filter dient zum Testen der zugehörigen Benutzeroberfläche zur Fehlerbehandlung. Es wird einen Fehler ausgeben, wenn ein bestimmtes Verb und eine Methode aufgerufen wird. Der Filter ist kein Problem. Das Problem ist die appsettings.configuration.So laden Sie ein untergeordnetes Objekt in appsetting.json (asp.net core)
Hier ist, was ich versuche zu tun: appsettings.development.json
"FaultTesting": {
"FaultRequests": false,
"SlowRequests": 0,
"FaultCalls": [
{
"Path": "/api/usercontext",
"Verbs": "get,put,delete"
},
{
"Path": "/api/cafeteriaaccounts",
"Verbs": "get"
}
]
}
Dies sind # -Typen meine c die Konfiguration zu halten:
public class FaultTestingOptions
{
/// <summary>
/// If true, checks FaultCalls for a path and verb to match.
/// </summary>
public bool FaultRequests { get; set; }
/// <summary>
/// Number of milliseconds to delay the response.
/// </summary>
public int SlowRequests { get; set; }
public FaultCall[] FaultCalls { get; set; }
}
public class FaultCall
{
public string Path { get; set; }
public string Verbs { get; set; }
}
hinzufügen, was in ich tue Start:
services.AddMvc(config =>
{
...
FaultTestingFilter(Options.Create(GetFaultTestingOptions())));
...
});
private FaultTestingOptions GetFaultTestingOptions()
{
var options = new FaultTestingOptions
{
FaultRequests = Configuration["FaultTesting:FaultRequests"].ToBoolean(),
SlowRequests = Convert.ToInt32(Configuration["FaultTesting:SlowRequests"])
};
var calls = Configuration.GetSection("FaultTesting:FaultCalls")
.GetChildren()
.Select(x => x.Value)
.ToArray();
var fooie = Configuration["FaultTesting:FaultCalls"];
//options.FaultCalls = calls.Select(c => new FaultCall { Path = c, Verbs = c.Value });
return options;
}
"Anrufe" ist ein Array von zwei Nullen, fooie ist null.
Was ist der richtige Ansatz hier?
Dies funktionierte sehr gut. Danke für die Lösung. – Elton