gpt4 book ai didi

asp.net-web-api - 网页 API : how to read action attribute and parameters from HttpContext

转载 作者:行者123 更新时间:2023-12-03 21:22:24 25 4
gpt4 key购买 nike

在普通课上,我需要从 HttpContext 中阅读以下内容:

  • Controller 和 Action 名称
  • Action 的属性(我可以通过 HttpActionContext.ActionDescriptor.GetCustomAttributes<type>() 获得,但这里我没有 HttpActionContext - 我只有 HttpContext )
  • 阅读参数(如 actionContext.ActionArguments["paramName"] ,但同样 - 我只有一个 HttpContext )

  • 它不是 Action 过滤器,也不是 Controller 类。但是,我可以访问 HttpContext .

    最佳答案

    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>();
    }

    关于asp.net-web-api - 网页 API : how to read action attribute and parameters from HttpContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747797/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com