forked from Geta/SEO.Sitemaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SitemapUrlRoutingInit.cs
66 lines (53 loc) · 1.97 KB
/
SitemapUrlRoutingInit.cs
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
65
66
using System.Web;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Web;
using Geta.SEO.Sitemaps.Services;
using InitializationModule = EPiServer.Web.InitializationModule;
namespace Geta.SEO.Sitemaps
{
[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class SitemapUrlRoutingInit : IInitializableModule
{
private readonly ISitemapRepository sitemapRepository;
public SitemapUrlRoutingInit()
: this(new SitemapRepository())
{
}
public SitemapUrlRoutingInit(ISitemapRepository sitemapRepository)
{
this.sitemapRepository = sitemapRepository;
}
public void Initialize(InitializationEngine context)
{
UrlRewriteModuleBase.HttpRewriteInit += HttpRewriteInit;
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(InitializationEngine context)
{
UrlRewriteModuleBase.HttpRewriteInit -= HttpRewriteInit;
}
private void HttpRewriteInit(object sender, UrlRewriteEventArgs e)
{
var urm = (UrlRewriteModule)sender;
urm.HttpRewritingToInternal += HttpRewritingToInternal;
}
public const string SitemapSessionKey = "SitemapDataSessionKey";
private void HttpRewritingToInternal(object sender, UrlRewriteEventArgs e)
{
if (e.Url.Path.ToLower().EndsWith("sitemap.xml"))
{
var sitemap = sitemapRepository.GetSitemapData(e.Url.ToString());
if (sitemap != null)
{
HttpContext.Current.Items.Add(SitemapSessionKey, sitemap);
e.UrlContext.InternalUrl.Path = "/Modules/Geta.SEO.Sitemaps/SitemapHandler.ashx";
e.IsModified = true;
}
}
}
}
}