using System;
using System.Web;
using System.Xml.Linq;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.web;
public class SiteMap : IHttpHandler
{
/* Generates an XML Sitemap for Umbraco using LinqToXml /
/ By Dan 'Diplo' Booth - http://www.diplo/co.uk/ */
private static readonly XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
public void ProcessRequest(HttpContext context)
{
// Set correct headers from XML
context.Response.ContentType = "text/xml";
context.Response.Charset = "utf-8";
// Get the absolute base URL for this website
Uri url = HttpContext.Current.Request.Url;
string baseUrl = String.Format("{0}://{1}{2}", url.Scheme, url.Host, url.IsDefaultPort ? "" : ":" + url.Port);
// Create a new XDocument using namespace and add root element
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
XElement urlset = new XElement(xmlns + "urlset");
// Get the root node
Node root = new Node(-1);
// Iterate all nodes in site and add them to document
RecurseNodes(urlset, root, baseUrl);
doc.Add(urlset);
// Write XML document to response stream
context.Response.Write(doc.Declaration + "\n");
context.Response.Write(doc.ToString());
}
// Method to recurse all nodes and create each element
private static void RecurseNodes(XElement urlset, Node node, string baseUrl)
{
foreach (Node n in node.Children)
{
// If the document has a property called "hidePage" set to true then ignore this node
if (n.GetProperty("hidePage") == null || n.GetProperty("hidePage").Value != "1")
{
if (n.GetProperty("hideInNav") != null)
{
string url = umbraco.library.NiceUrl(n.Id);
// Tidy up home page so it's more canonical
if (url.EndsWith("/home.aspx"))
url = url.Replace("/home.aspx", "/");
// Create the XML node
XElement urlNode = new XElement(xmlns + "url", new XElement(xmlns + "loc", url), new XElement(xmlns + "lastmod", n.UpdateDate.ToUniversalTime()));
//XElement urlNode = new XElement(xmlns + "url", new XElement(xmlns + "loc", baseUrl + url), new XElement(xmlns + "lastmod", n.UpdateDate.ToUniversalTime()));
urlset.Add(urlNode);
}
}
// Check if the node has any child nodes and, if it has, recurse them
if (node.Children != null && node.Children.Count > 0)
RecurseNodes(urlset, n, baseUrl);
}
}
public bool IsReusable
{
get
{
return false;
}
}
public static string GetDomain(int pageId)
{
string domainList = "";
try
{
Domain[] domains = umbraco.library.GetCurrentDomains(pageId);
if (domains != null)
{
foreach (Domain d in domains)
{
if (domainList.Length > 0)
{domainList += ' ';}
domainList += d.Name;
}
return domainList;
}
}
catch (Exception errDictionary)
{
}
return string.Empty;
}
Get root node by domain name
Hello, i have sitemap.ashx and many domain on one install of Umbraco.
I need root node by domain name for create correct sitemap.
For example:
www.1.com
www.2.com
All sites start from one type of documents alias: "MainPage"
Thanks.
Hi,
Do you want this in the API, Razor or XSLT?
Rich
Hi, its .ashx
Code:
<%@ WebHandler Language="C#" Class="SiteMap" %>
using System; using System.Web; using System.Xml.Linq; using umbraco.presentation.nodeFactory; using umbraco.cms.businesslogic.web;
public class SiteMap : IHttpHandler { /* Generates an XML Sitemap for Umbraco using LinqToXml / / By Dan 'Diplo' Booth - http://www.diplo/co.uk/ */
}
Problem solved.
add in ashx
try this
Uri myUri = new Uri("http://forums.asp.net/t/1110512.aspx?");
string host = myUri.Host;
and some other methods also you can use...check this link
http://net-informations.com/faq/asp/domain.htm
steve
is working on a reply...