WebAPI HttpActionExecutedContext获取控制器名称

时间:2014-08-14 13:54:35

标签: c# asp.net-web-api asp.net-web-api2

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

我有以下过滤器:

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"];

有任何线索吗?欣赏它

1 个答案:

答案 0 :(得分:48)

最后我找到了它:

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

由于

相关问题