VAA*_*AAA 22 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;
}
}
Run Code Online (Sandbox Code Playgroud)
在传统的MVC中,这很简单:
string controllerName = (string) filterContext.RouteData.Values["controller"];
string actionName = (string) filterContext.RouteData.Values["action"];
Run Code Online (Sandbox Code Playgroud)
任何线索?欣赏它
VAA*_*AAA 49
最后我找到了:
filterContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName
filterContext.ActionContext.ActionDescriptor.ActionName
Run Code Online (Sandbox Code Playgroud)
谢谢