gpt4 book ai didi

c# - WebAPI HttpActionExecutedContext 获取 Controller 名称

转载 作者:IT王子 更新时间:2023-10-29 04:29:12 27 4
gpt4 key购买 nike

我需要获取触发过滤器属性的 Controller 。

我有以下过滤器:

public override void OnException(HttpActionExecutedContext filterContext) {
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}


if (filterContext.Exception != null) {

// string controllerName = (string) filterContext.....??

// string actionName = (string) filterContext.....?

HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.InternalServerError) {
Content = new StringContent("An unhandled exception was thrown by Customer Web API controller."),
ReasonPhrase = "An unhandled exception was thrown by Customer Web API controller."
};

filterContext.Response = msg;


}

}

在传统的 MVC 中,这很容易做到:

string controllerName = (string) filterContext.RouteData.Values["controller"];
string actionName = (string) filterContext.RouteData.Values["action"];

有什么线索吗?欣赏一下

最佳答案

终于找到了:

filterContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName
filterContext.ActionContext.ActionDescriptor.ActionName

谢谢

关于c# - WebAPI HttpActionExecutedContext 获取 Controller 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25309829/

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