Page 1 of 1

So erhalten Sie den Aktionskontext in einer Anfrage

Posted: 01 Jan 2025, 23:36
by Anonymous
Wie erhalte ich einen ActionContext aus der aktuellen Anfrage?
Ich habe verschiedene Dinge ausprobiert, wie zum Beispiel das Einfügen eines ActionContextAccessor wie unten und dessen Verwendung, aber es gibt immer null zurück.

Code: Select all

builder.Services.AddSingleton();

var ctx = HttpContext.RequestServices.GetRequiredService()
Ich versuche auch, den ActionContext selbst zu erstellen, aber dann kann ich seine Funktionen wie die Deskriptoren des aktuellen Controllers und der Aktion nicht abrufen
< pre class="lang-cs Prettyprint-override">

Code: Select all

var actionContext = new ActionContext(HttpContext, HttpContext.GetRouteData(), new ActionDescriptor());

// then when I do something like this I get nothing

var httpAttributes = actionContext.ActionDescriptor.GetAttributes().GetAwaiter().GetResult();

string entity = (actionContext.ActionDescriptor?.GetAttributes().GetAwaiter().GetResult()).FirstOrDefault()?.Name;