My goal is to log some controller action with an Id.
Then I have created a LogFilter to use it as a method attribute
public class LogFilter : ActionFilterAttribute, IActionFilter
{
public TypeLog Type { get; set; }
public String Libelle { get; set; }
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
int idhospit = 0;
object retour = filterContext.Result as JsonDotNetResult;
if (retour == null)
retour = filterContext.Result as JsonResult;
if (retour == null)
retour = filterContext.Result as PartialViewResult;
object data = null;
bool success = false;
if (retour != null && retour is PartialViewResult)
{
PartialViewResult pvr = retour as PartialViewResult;
data = new { Success = true, id = pvr.ViewData["id"] };
}
else if (retour != null)
{
data = retour.GetType().GetProperty("Data").GetValue(retour, null);
}
if (retour != null
&& data != null
&& data.GetType().GetProperty("Success") != null
&& data.GetType().GetProperty("Success").GetValue(data, null) != null)
{
success = (bool)(data.GetType().GetProperty("Success").GetValue(data, null));
}
if (success
&& retour != null
&& data != null
&& data.GetType().GetProperty("id") != null
&& data.GetType().GetProperty("id").GetValue(data, null) != null)
{
var id = (data.GetType().GetProperty("id").GetValue(data, null));
idhospit = (int)id;
GPL.Bo.Models.Log log = new Bo.Models.Log();
log.int_id = SessionHelper.IntervenantId;
log.log_action = filterContext.ActionDescriptor.ActionName;
log.log_controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
log.log_date = filterContext.HttpContext.Timestamp;
log.hospit_id = idhospit;
log.log_libelle = Libelle;
log.log_type = Type.ToString();
GPL.Services.ServiceLog SvcLog = new GPL.Services.ServiceLog(Helpers.APP_CODE, SessionHelper.SiteCode, SessionHelper.IntervenantId);
SvcLog.AjouterLog(log);
}
base.OnActionExecuted(filterContext);
}
}
My issue is on PartialViewResult type. I have saved "id" value in TempData / ViewData / ViewBag, but it is ever null in LogFilter.
[LogFilter(Type = TypeLog.Ajout, Libelle = "Ajout/Modification hospitalisation")]
public ActionResult AjoutHospitalisation(string ufsCode, DateTime dateDebutCalendrier, DateTime dateFinCalendrier, TypeView viewType,
GPL_Hospitalisation model, FormCollection collection)
{
//save or update and bussiness logic ....
//then return view
AgendaController ac = new AgendaController();
TempData["id"] = t.Item3;
ViewData["id"] = t.Item3;
ViewBag.id = t.Item3;
//Ask another controller to send PatialViewResult
return ac.Jour(ufsCode, dateDebutCalendrier);
}
So what is the good method to send some parameters in actionFilter ? Thank you for helping.