Kõigepealt lisame mõned read Web.config faili
<system.web> <siteMap defaultProvider="MyProvider"> <providers> <clear/> <add name="MyProvider" type="MinuOmaNimeruum.RoutingSiteMapProvider"/> </providers> </siteMap> <httpHandlers> <add verb="*" path="sitemap.axd" type="Newtonsoft.Sitemaps.SitemapHandler, Newtonsoft.Sitemaps"/> </httpHandlers> </system.web>
Teiseks peame me tirima Sitemaps.NET plugina ja paigaldama selle Bin kausta. Järgmiseks paiguta endale meelepärasesse kohta väikene fail mis täiendab ASP.NET Sitemap mootorit nii, et see töötab kenasti ka ASP.NET MVC’ga ja on dünaamiline (andmed võtab ta routing datast).
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Web.Hosting; using System.Xml; using System.Collections.Specialized; namespace MinuOmaNimeruum { public class RoutingSiteMapProvider : StaticSiteMapProvider { private SiteMapNode rootNode; public override void Initialize(string name, NameValueCollection attributes) { base.Initialize(name, attributes); // Load XML file, taking name from Web.config or use Web.sitemap as default var xmlDoc = new XmlDocument(); var siteMapFile = attributes["siteMapFile"] ?? "~/Web.sitemap"; xmlDoc.Load(HostingEnvironment.MapPath(siteMapFile)); var rootSiteMapNode = xmlDoc.DocumentElement["siteMapNode"]; // Build the navigation structure var httpContext = new HttpContextWrapper(HttpContext.Current); var requestContext = new RequestContext(httpContext, new RouteData()); rootNode = AddNodeRecursive(rootSiteMapNode, null, requestContext); } private static string[] reservedNames = new[] { "title", "description", "roles" }; private SiteMapNode AddNodeRecursive(XmlNode xmlNode, SiteMapNode parent, RequestContext context) { // Generate this node's URL by querying RouteTable.Routes var routeValues = (from XmlNode attrib in xmlNode.Attributes where !reservedNames.Contains(attrib.Name.ToLower()) select new { attrib.Name, attrib.Value }) .ToDictionary(x => x.Name, x => (object)x.Value); var routeDict = new RouteValueDictionary(routeValues); var url = RouteTable.Routes.GetVirtualPath(context, routeDict).VirtualPath; // Register this node and its children var title = xmlNode.Attributes["title"].Value; var node = new SiteMapNode(this, Guid.NewGuid().ToString(), url, title); base.AddNode(node, parent); foreach (XmlNode childNode in xmlNode.ChildNodes) AddNodeRecursive(childNode, node, context); return node; } // These methods are called by ASP.NET to fetch your site map data protected override SiteMapNode GetRootNodeCore() { return rootNode; } public override SiteMapNode BuildSiteMap() { return rootNode; } } }
Nüüd on meil vaja vaid lisada projekti Web.Sitemap fail mille näidis sisu võiks olla midagi sellist.
<?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode title="Home" controller="Home" action="Index"> <siteMapNode title="About" controller="Home" action="About"/> <siteMapNode title="Log in" controller="Account" action="LogOn"/> </siteMapNode> </siteMap>
Ma arvan, et siitmaalt saad sa juba ise väga kenasti edasi liikuda, kõik peaks olema tuttav. Sitemap on nüüd kättesaadav kenasti http://sinudomeen.ee/sitemap.axd faili kaudu, selle võid sa rahuliku südamega Googlile ette sööta.
PS: Kui sul hakkab IIS kõige selle peale karjuma, tasuks AppPool ümber seadistada Classicu peale Managed Pipeline alt.