ActionFilter在RedirectToAction之前读取内容

时间:2012-05-24 12:41:59

标签: asp.net-mvc-3 entity-framework-4.1

我希望记录删除和编辑,并认为最好的方法是应用一个actionFilter 我删除和编辑[post]方法的属性

但是因为最终结果是重定向到动作,所以我的Context.Result始终为null 因为只有Context.RedirectToAction结果可用。

在我创建一些代码插入我的删除和编辑功能之前,有没有人试过这样的东西!,你可以建议吗?

由于

行动守则:

[HttpPost, ValidateInput(false)]
    [SiteChangeLogger(LogType = "Update", TableName = "Affiliates")]
    public ActionResult Edit(Affiliate affiliate, FormCollection form)
    {
        var existing = db2.Affiliates.SingleOrDefault(x => x.AffiliateId == affiliate.AffiliateId);
        ViewBag.before = Common.Strings.Base64Encode(Common.Strings.ToJsonString(existing));
        if (ModelState.IsValid)
        {
            try
            {
                var curFiles = new NameValueCollection();
                curFiles["AffiliateLogo"] = affiliate.AffiliateLogo;
                if (!String.IsNullOrWhiteSpace(form["AffiliateLogo"]))
                {
                     UploadFiles(form,curFiles);
                     TryUpdateModel(affiliate, form);

                     var oldFileName = affiliate.AffiliateLogo;
                     var newFileName = Common.Strings.RandomFileName();
                     new WebImage(Server.MapPath("~/Content/images/" + affiliate.AffiliateLogo))
                         .Resize(200, 50, true, true)
                         .Crop(1, 1)
                         .Save(Server.MapPath("~/Content/images/" + newFileName), "png", true);
                     affiliate.AffiliateLogo = newFileName + ".png";
                     Common.Common.TryAndDeleteFile("~/Content/images/" + oldFileName);
                }
                else
                {
                    affiliate.AffiliateLogo = existing.AffiliateLogo;
                }

            }
            catch (Exception ex)
            {
                Common.Common.CompileErrorMessage(ex,"ADMIN.Affiliate.Edit");
            }
            finally
            {
                db.Entry(affiliate).State = EntityState.Modified;
                db.SaveChanges();   
            }
            ViewBag.after = Common.Strings.Base64Encode(Common.Strings.ToJsonString(affiliate));
            return RedirectToAction("Index");
        }
        return View(affiliate);
    }

我的过滤代码

public override void OnResultExecuted(ResultExecutedContext fc)
    {
        var viewResult = fc.Result as ViewResult;

        if(viewResult == null) return;

        var beforeData = viewResult.ViewBag.before;
        var afterData = viewResult.ViewBag.after;

        if (beforeData == null && afterData == null) return;

        var ctx = new SgeGamesContext();
        var eventId = 0;
        var siteChangeLogEvent = ctx.SiteChangeLogEvents.SingleOrDefault(x => x.SiteChangeLogEventName == LogType);
        if (siteChangeLogEvent != null)
        {
            eventId = siteChangeLogEvent.SiteChangeLogEventId;
        }

        var model = new Sge.Games.Data.Models.SiteChangeLog
                        {
                            SiteChangeLogTable = TableName,
                            SiteId = 1,
                            SiteChangeLogAfterContent = afterData,
                            SiteChangeLogBeforeContent = beforeData,
                            SiteChangeLogEventId = eventId
                        };
        ctx.SiteChangeLogs.Add(model);
        ctx.SaveChanges();

        base.OnResultExecuted(fc);
    }

1 个答案:

答案 0 :(得分:2)

您可以直接访问ViewBag,不需要ViewResult:

public override void OnResultExecuted(ResultExecutedContext fc)
{
    var before = fc.Controller.ViewBag.before;
    var after = fc.Controller.ViewBag.after;
    ...
}

此外,您可能希望使用OnActionExecuted事件而不是OnResultExecuted