WebAPI HttpActionExecutedContext コントローラー名を取得する 質問する

WebAPI HttpActionExecutedContext コントローラー名を取得する 質問する

フィルター属性をトリガーするコントローラーを取得する必要があります。

次のフィルターがあります:

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

ついに私はそれを見つけました:

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

ありがとう

おすすめ記事