[英]Web API: how to read action attribute and parameters from HttpContext
在常規課程中,我需要從HttpContext
閱讀以下內容:
控制器和動作名稱
Action 的屬性(我可以通過HttpActionContext.ActionDescriptor.GetCustomAttributes<type>()
獲得它,但在這里我沒有HttpActionContext
- 我只有HttpContext
)
讀取參數(如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.