Web API: how to read action attribute and parameters from HttpContext
Asked Answered
S

1

7

In regular class, I need to read following from the HttpContext:

  1. Controller and action name

  2. Action's attribute (I could get that through HttpActionContext.ActionDescriptor.GetCustomAttributes<type>() but here I don't have HttpActionContext - I only have HttpContext)

  3. Read argument (like actionContext.ActionArguments["paramName"], but again - I only have a HttpContext)

It's not an action filter and not a controller class. But, I can access HttpContext.

Scalariform answered 7/6, 2018 at 18:19 Comment(2)
Have you checked under HttpContext.Current.Request.Form?Refectory
ya, I can not find the value (posted value) that I could see under actionContext.ActionArguments["paramName"].Scalariform
N
13

From asp.net core 3.0 https://mcmap.net/q/219738/-how-to-read-action-method-39-s-attributes-in-asp-net-core-mvc

public async Task Invoke(HttpContext context)
{
    // Get the enpoint which is executing (asp.net core 3.0 only)
    var executingEnpoint = context.GetEndpoint();

    // Get attributes on the executing action method and it's defining controller class
    var attributes = executingEnpoint.Metadata.OfType<MyCustomAttribute>();

    await next(context);

    // Get the enpoint which was executed (asp.net core 2.2 possible after call to await next(context))
    var executingEnpoint2 = context.GetEndpoint();

    // Get attributes on the executing action method and it's defining controller class
    var attributes2 = executingEnpoint.Metadata.OfType<MyCustomAttribute>();
}
Nashbar answered 5/8, 2020 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.