8

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.

2
  • Have you checked under HttpContext.Current.Request.Form? Commented Jun 7, 2018 at 19:52
  • ya, I can not find the value (posted value) that I could see under actionContext.ActionArguments["paramName"]. Commented Jun 7, 2018 at 20:00

1 Answer 1

14

From asp.net core 3.0 https://stackoverflow.com/a/60602828/10612695

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>();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.