我正在寻找一种使用动作过滤器的方法,该过滤器可以记录有关从另一个动作调用的方法/动作的信息。这可能吗?
我想举例说明我试图让它发挥作用:
我有一个名为Create
的行为来自CampaignsController
,并且在该行动中我称之为另一个"行动/方法"我希望获取信息。
到目前为止我找不到了:
动作过滤器仅适用于扩展Controller
的类。
我无法对在另一个操作中调用的操作/方法使用过滤器。
我可以将信息放在TempData中,之后在动作过滤器中访问它,但我不知道这是否是最佳解决方案。
感谢。
我使用的代码如下:
CampaingsController
namespace GBD_01.Controllers
{
[SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]
public class CampaignsController : Controller
{
private GBD.FrontOffice.Controllers.FieldsController controller = new GBD.FrontOffice.Controllers.FieldsController();
[HttpGet]
public ActionResult Create(string returnUrl = "")
{
ViewBag.returnUrl = returnUrl;
return View();
}
[HttpPost]
public ActionResult Create(Campaign model, string returnUrl)
{
if (model.Type == "EMAIL")
{
ModelState.Remove("message");
}
else if(model.Type == "SMS")
{
ModelState.Remove("URL");
}
if (ModelState.IsValid)
{
EgoiClass egoiCls = new EgoiClass();
if (model.Type == "EMAIL")
{
egoiCls.createCampaignEmail(model.CampaignName, model.Subject, model.URL);
}
else if (model.Type == "SMS")
egoiCls.createCampaignSMS(model.CampaignName, model.Subject, model.message, model.URL);
}
return View(model);
}
被调用对象的方法:
namespace GBD_01.Controllers
{
public class EgoiController
{
public Dictionary<string, object> createCampaignEmail(string subject, string internal_Name, string url)
{
try
{
string apikey = vars.getKeyValue("EgoiApiKey");
if (!String.IsNullOrEmpty(apikey))
{
GBD.FrontOffice.Controllers.FieldsController fieldCtrl = new GBD.FrontOffice.Controllers.FieldsController();
EgoiApi api = EgoiApiFactory.getApi(Protocol.XmlRpc);
EgoiMap map = new EgoiMap();
EgoiMap mapResult = new EgoiMap();
string senderCode = vars.getKeyValue("EgoiSender");/*ProjectSession.EgoiSenderCode*/;
map.Add("apikey", apikey);
map.Add("from", senderCode);
if (!String.IsNullOrEmpty(subject))
map.Add("subject", subject);
/* OPTIONS FOR LINKS IN NEWSLETTERS*/
if (!string.IsNullOrEmpty(vars.getKeyValue("EgoiLinks")))
{
string[] links = vars.getKeyValue("EgoiLinks").Split(';');
foreach (string s in links)
{
map.Add(s, "1");
}
}
if (!String.IsNullOrEmpty(internal_Name))
map.Add("internal_name", internal_Name);
if (!String.IsNullOrEmpty(url))
map.Add("url", url);
map.Add("listID", vars.getKeyValue("EgoiContactListId"));
mapResult = api.createCampaignEmail(map);
Dictionary<string, object> item = (Dictionary<string, object>)mapResult;
if (!item.ContainsKey("ERROR"))
return true;
else
return false;
}
}
catch (Exception ex) { }
return false;
}
}
}
我对动作过滤器的意思是:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class LogActionResult : ActionFilterAttribute //TESTE - os action filter apenas funcion em controladores e acções
{
private static readonly ILog log = LogManager.GetLogger(typeof(LogActionResult).Name);
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
ActionExecutedContext context = filterContext;
StringBuilder logMessage = new StringBuilder();
Log("OnActionExecuting", filterContext.RouteData);
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
Log("OnResultExecuted", filterContext.RouteData);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
Log("OnResultExecuting ", filterContext.RouteData);
}
private void Log(string methodName, RouteData routeData)
{
var controllerName = routeData.Values["controller"];
var actionName = routeData.Values["action"];
var message = String.Format("{0}- controller:{1} action:{2}", methodName,
controllerName,
actionName);
Debug.WriteLine(message);
}
}
我想使用有一个日志包来保存有关发生的事情的信息以及有关api调用的信息:
log4net.ILog logger = log4net.LogManager.GetLogger("LoginLog");
logger.Info("Acção: action name | result: '"+mapResult+"' | logged at " + DateTime.Now);