Created
August 19, 2015 09:52
-
-
Save zdarovka/15831bf3a520a06b6f76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult SiteMap() | |
{ | |
var items = new List<SiteMapItem> | |
{ | |
new SiteMapItem("Index", "Serial", SiteMapChangeFreq.Daily, 1.0f), | |
new SiteMapItem("About", "Home", SiteMapChangeFreq.Monthly, 1.0f), | |
new SiteMapItem("Register", "Account", SiteMapChangeFreq.Monthly, 0.9f), | |
new SiteMapItem("Login", "Account", SiteMapChangeFreq.Monthly, 0.9f), | |
new SiteMapItem("Changelog", "Home", SiteMapChangeFreq.Monthly, 0.9f), | |
new SiteMapItem("Donate", "Home", SiteMapChangeFreq.Never, 0.9f) | |
}; | |
RepositorySpecial.FetchAllSerialIds().ForEach(i => | |
items.Add(new SiteMapItem("Detail", "Serial", new { id = i }, SiteMapChangeFreq.Weekly, 0.8f))); | |
int episodesCount = RepositoriesFactory.EpisodeRepository.Count(new ReleasedEpisodesQuery()); | |
int pages = episodesCount / DEFAULT_PAGE_SIZE + (episodesCount % DEFAULT_PAGE_SIZE == 0 ? 0 : 1); | |
for (int i = 2; i <= pages; i++) | |
{ | |
items.Add(new SiteMapItem("Index", "Serial", new { id = i }, SiteMapChangeFreq.Daily, 0.7f)); | |
} | |
return new SiteMapResult(items); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace SerialTracker.Web.SiteMap | |
{ | |
public interface ISiteMapItem | |
{ | |
string GetAddress(UrlHelper Url, string siteAddress); | |
string ChangeFreq | |
{ | |
get; | |
set; | |
} | |
float Priority | |
{ | |
get; | |
set; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SerialTracker.Web.SiteMap | |
{ | |
public static class SiteMapChangeFreq | |
{ | |
public static string Always | |
{ | |
get | |
{ | |
return "always"; | |
} | |
} | |
public static string Hourly | |
{ | |
get | |
{ | |
return "hourly"; | |
} | |
} | |
public static string Daily | |
{ | |
get | |
{ | |
return "daily"; | |
} | |
} | |
public static string Weekly | |
{ | |
get | |
{ | |
return "weekly"; | |
} | |
} | |
public static string Monthly | |
{ | |
get | |
{ | |
return "monthly"; | |
} | |
} | |
public static string Yearly | |
{ | |
get | |
{ | |
return "yearly"; | |
} | |
} | |
public static string Never | |
{ | |
get | |
{ | |
return "never"; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace SerialTracker.Web.SiteMap | |
{ | |
public class using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using System.Xml; | |
using System.Web; | |
namespace SerialTracker.Web.SiteMap | |
{ | |
public class SiteMapResult : ActionResult | |
{ | |
private List<ISiteMapItem> _items; | |
public SiteMapResult(IEnumerable<ISiteMapItem> items) | |
:this(items.ToList()) | |
{ | |
} | |
public SiteMapResult(List<ISiteMapItem> items) | |
{ | |
_items = items; | |
} | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
XmlWriterSettings settings = new XmlWriterSettings(); | |
UrlHelper Url = new UrlHelper(context.HttpContext.Request.RequestContext); | |
settings.Indent = true; | |
settings.NewLineHandling = NewLineHandling.Entitize; | |
context.HttpContext.Response.ContentType = "text/xml"; | |
using (XmlWriter _writer = XmlWriter.Create(context.HttpContext.Response.OutputStream, settings)) | |
{ | |
// Begin structure | |
_writer.WriteStartElement("urlset","http://www.sitemaps.org/schemas/sitemap/0.9"); | |
_writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); | |
_writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"); | |
// Individual items | |
_items.ForEach(x => | |
{ | |
_writer.WriteStartElement("url"); | |
_writer.WriteElementString("loc", | |
x.GetAddress(Url, context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority))); | |
_writer.WriteElementString("changefreq", x.ChangeFreq); | |
_writer.WriteElementString("priority", x.Priority.ToString("0.00").Replace(",", ".")); | |
_writer.WriteEndElement(); | |
}); | |
// End structure | |
_writer.WriteEndElement(); | |
} | |
} | |
} | |
} : ISiteMapItem | |
{ | |
public SiteMapItem(string action, string controller, string changefreq, float prior = 0.5f) | |
: this(action, controller, null, changefreq, prior) | |
{ | |
} | |
public SiteMapItem(string action, string controller, object routevalues, string changefreq, float prior = 0.5f) | |
{ | |
this.Action = action; | |
this.Controller = controller; | |
this.RouteValues = routevalues; | |
this.ChangeFreq = changefreq; | |
this.Priority = prior; | |
} | |
public string GetAddress(UrlHelper Url, string siteAddress) | |
{ | |
return siteAddress + Url.Action(Action, Controller, RouteValues); | |
} | |
public string Action | |
{ | |
get; | |
set; | |
} | |
public string Controller | |
{ | |
get; | |
set; | |
} | |
public object RouteValues | |
{ | |
get; | |
set; | |
} | |
public string ChangeFreq | |
{ | |
get; | |
set; | |
} | |
public float Priority | |
{ | |
get; | |
set; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using System.Xml; | |
using System.Web; | |
namespace SerialTracker.Web.SiteMap | |
{ | |
public class SiteMapResult : ActionResult | |
{ | |
private List<ISiteMapItem> _items; | |
public SiteMapResult(IEnumerable<ISiteMapItem> items) | |
:this(items.ToList()) | |
{ | |
} | |
public SiteMapResult(List<ISiteMapItem> items) | |
{ | |
_items = items; | |
} | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
XmlWriterSettings settings = new XmlWriterSettings(); | |
UrlHelper Url = new UrlHelper(context.HttpContext.Request.RequestContext); | |
settings.Indent = true; | |
settings.NewLineHandling = NewLineHandling.Entitize; | |
context.HttpContext.Response.ContentType = "text/xml"; | |
using (XmlWriter _writer = XmlWriter.Create(context.HttpContext.Response.OutputStream, settings)) | |
{ | |
// Begin structure | |
_writer.WriteStartElement("urlset","http://www.sitemaps.org/schemas/sitemap/0.9"); | |
_writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); | |
_writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"); | |
// Individual items | |
_items.ForEach(x => | |
{ | |
_writer.WriteStartElement("url"); | |
_writer.WriteElementString("loc", | |
x.GetAddress(Url, context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority))); | |
_writer.WriteElementString("changefreq", x.ChangeFreq); | |
_writer.WriteElementString("priority", x.Priority.ToString("0.00").Replace(",", ".")); | |
_writer.WriteEndElement(); | |
}); | |
// End structure | |
_writer.WriteEndElement(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment