Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Grom 11 posts 91 karma points
    Aug 29, 2013 @ 05:22
    Grom
    0

    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.

  • Rich Green 2246 posts 4006 karma points
    Aug 29, 2013 @ 08:23
    Rich Green
    0

    Hi,

    Do you want this in the API, Razor or XSLT?

    Rich

  • Grom 11 posts 91 karma points
    Aug 29, 2013 @ 09:44
    Grom
    0

    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/ */

    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;
    }
    

    }

  • Grom 11 posts 91 karma points
    Sep 03, 2013 @ 09:29
    Grom
    100

    Problem solved.

    add in ashx

        using umbraco;
        using System.Collections.Generic;
        using System.Linq;
        using System.Net;
    
            Node root = new Node(GetNodebyHost(int rootNode, string Alias, string Host));
    
            int GetNodebyHost(int rootNode, string Alias, string Host)
                {
                    int curNode = -1;
                    umbraco.interfaces.INode parent = uQuery.GetNode(rootNode);
                    List<umbraco.interfaces.INode> nodes = parent.GetDescendantNodesByType(Alias).ToList();
                    foreach (umbraco.interfaces.INode node in nodes)
                    {
                        Uri a = new Uri(node.NiceUrl);
                        if (a.Host ==  Host)
                        {
                            curNode = node.Id;
                        }
                    }
    
                    return curNode;
                }
    
  • steve 1 post 21 karma points
    Apr 10, 2014 @ 09:09
    steve
    0

     

    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

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies