Repository navigation
Expand file tree
/
Copy pathNotifyAttribute.cs
More file actions
64 lines (50 loc) · 1.74 KB
/
Copy pathNotifyAttribute.cs
File metadata and controls
64 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Mvc;
using SmartStore.Collections;
using SmartStore.Core.Infrastructure;
using SmartStore.Core.Localization;
using SmartStore.Core.Logging;
namespace SmartStore.Web.Framework.Controllers
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class NotifyAttribute : ActionFilterAttribute
{
internal const string NotificationsKey = "sm.notifications.all";
public INotifier Notifier { get; set; }
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (Notifier == null || !Notifier.Entries.Any())
return;
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
HandleAjaxRequest(Notifier.Entries.FirstOrDefault(), filterContext.HttpContext.Response);
return;
}
Persist(filterContext.Controller.ViewData, Notifier.Entries.Where(x => x.Durable == false));
Persist(filterContext.Controller.TempData, Notifier.Entries.Where(x => x.Durable == true));
}
private void Persist(IDictionary<string, object> bag, IEnumerable<NotifyEntry> source)
{
if (!source.Any())
return;
var existing = (bag[NotificationsKey] ?? new List<NotifyEntry>()) as List<NotifyEntry>;
source.Each(x => {
if (x.Message.Text.HasValue() && !existing.Contains(x))
existing.Add(x);
});
bag[NotificationsKey] = existing;
}
private void HandleAjaxRequest(NotifyEntry entry, HttpResponseBase response)
{
if (entry == null)
return;
response.AddHeader("X-Message-Type", entry.Type.ToString().ToLower());
response.AddHeader("X-Message", entry.Message.Text);
}
}
}