web-dev-qa-db-ja.com

WebAPIHttpActionExecutedContextはコントローラー名を取得します

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

私は次のフィルターを持っています:

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

どんな手掛かり?感謝します

23
VAAA

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

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

ありがとう

52
VAAA