我有以下的Web API ActionFilterAttribute
的WebAPI ActionFilterAttribute OnExecuting不點火
namespace namespace.Filters {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
public class LogApiRequestActionFilterAttribute : ActionFilterAttribute {
public LogApiRequestActionFilterAttribute() {
}
private void logData(HttpActionContext actionContext) {
var controllerName = actionContext.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;
var actionName = actionContext.ActionDescriptor.ActionName;
var parameters = "";
foreach (var item in actionContext.ActionArguments) {
parameters += string.Format("{0} = {1}, ", item.Key, item.Value);
}
if (parameters.Any()) {
parameters = parameters.Remove(parameters.Count() - 2);
}
var message = string.Format("{0}.{1}({2})", controllerName, actionName, parameters);
// Do the logging
}
public override void OnActionExecuting(HttpActionContext actionContext) {
logData(actionContext);
base.OnActionExecuting(actionContext);
}
}
}
當我在全球它添加在WebApiConfig.cs
這樣的:
config.Filters.Add(new LogApiRequestActionFilterAttribute());
但logData
方法不會被調用。有誰知道爲什麼?
應該將'actionContext'參數的類型改爲'ActionExecutingContext'而不是? – DLeh
@DLeh,這是Visual Studio如何爲我實現接口:) – Knerd
對於一個非常基本的問題感到抱歉,但你怎麼知道它沒有達到那裏? – harishr