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

标签 asp.net-web-api asp.net-web-api2 httpcontext actioncontext routedata

在普通课上,我需要从 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/

    相关文章:

    xamarin - xamarin.forms 中的数据发布错误

    c# - 如何在 fiddler 中捕获服务器端 web api 调用?

    c# - 如果无论如何都要结束,是否有必要在 Application_End 上进行处理?

    c# - 在自定义 TypeFormatter 中使用 RegisterWebApiRequest 时出现 SimpleInjector.ActivationException

    c# - 有没有一种简单的方法可以让 ClassLibrary 获取 session 值?

    c# - .net webapi 文件路径作为参数

    typescript - 具有基本安全授权的 Angular 2 调用 API

    asp.net-mvc-4 - 如何以代码优先方法在现有数据库表中添加列?

    c# - 在 ASP.NET 中为特定页面设置无缓存?

    c# - HttpContext.Session 不维护状态