簡體   English   中英

Web API:如何從 HttpContext 讀取動作屬性和參數

[英]Web API: how to read action attribute and parameters from HttpContext

在常規課程中,我需要從HttpContext閱讀以下內容:

  1. 控制器和動作名稱

  2. Action 的屬性(我可以通過HttpActionContext.ActionDescriptor.GetCustomAttributes<type>()獲得它,但在這里我沒有HttpActionContext - 我只有HttpContext

  3. 讀取參數(如actionContext.ActionArguments["paramName"] ,但同樣 - 我只有一個HttpContext

它不是動作過濾器,也不是控制器類。 但是,我可以訪問HttpContext

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM