تولید SiteMap استاندارد و ایجاد یک ActionResult اختصاصی برای Return کردن SiteMap تولید شده
نویسنده: مهران موسوی
تاریخ: ۱۳۹۲/۰۲/۲۹ ۲۱:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections;
using System.Web.Mvc;
using System.Xml.Serialization;
namespace Neoox.Core.SeoTools
{
[XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public class Sitemap
{
private ArrayList map;
public Sitemap()
{
map = new ArrayList();
}
[XmlElement("url")]
public Location[] Locations
{
get
{
Location[] items = new Location[map.Count];
map.CopyTo(items);
return items;
}
set
{
if (value == null)
return;
Location[] items = (Location[])value;
map.Clear();
foreach (Location item in items)
map.Add(item);
}
}
public int Add(Location item)
{
return map.Add(item);
}
}
public class Location
{
public enum eChangeFrequency
{
always,
hourly,
daily,
weekly,
monthly,
yearly,
never
}
[XmlElement("loc")]
public string Url { get; set; }
[XmlElement("changefreq")]
public eChangeFrequency? ChangeFrequency { get; set; }
public bool ShouldSerializeChangeFrequency() { return ChangeFrequency.HasValue; }
[XmlElement("lastmod")]
public DateTime? LastModified { get; set; }
public bool ShouldSerializeLastModified() { return LastModified.HasValue; }
[XmlElement("priority")]
public double? Priority { get; set; }
public bool ShouldSerializePriority() { return Priority.HasValue; }
}
public class XmlResult : ActionResult
{
private object objectToSerialize;
public XmlResult(object objectToSerialize)
{
this.objectToSerialize = objectToSerialize;
}
public object ObjectToSerialize
{
get { return this.objectToSerialize; }
}
public override void ExecuteResult(ControllerContext context)
{
if (this.objectToSerialize != null)
{
context.HttpContext.Response.Clear();
var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
context.HttpContext.Response.ContentType = "text/xml";
xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
}
}
}
} public ActionResult Sitemap()
{
Sitemap sm = new Sitemap();
sm.Add(new Location()
{
Url = string.Format("http://www.TechnoDesign.ir/Articles/{0}/{1}", 1, "SEO-in-ASP.NET-MVC"),
LastModified = DateTime.UtcNow,
Priority = 0.5D
});
return new XmlResult(sm);
}routes.MapRoute(
"SiteMap_route", // Route name
"sitemap.xml", // URL with parameters
new { controller = "Sitemap", action = "index", name = UrlParameter.Optional, area = "" } // Parameter defaults
);in robots.txt file: Sitemap: http://www.site.com/Sitemap
public void ProcessRequest (HttpContext context)
public virtual ActionResult Sitemap()
{
var data = new sunn.Models.ApplicationDbContext().Posts.ToList();
SiteMap sm = new SiteMap();
foreach (var siteno in data)
{
sm.Add(new Location()
{
Url = string.Format("http://www.MySite.ir/Develop/Home/Post/{0}", siteno.Id),
LastModified = siteno.InsertDate,
Priority = 0.5D
});
}
return new XmlResult(sm);
} خوانایی این خروجی اهمیتی نداره چون برای ماشین درست میشه نه برای انسان. فرمت هم نداشته باشه بهتره چون حجم کمتری رو به خودش اختصاص میده. ولی در کل باید خاصیت Indent تنظیم بشه، در صورت نیاز.
در مورد خروجی با پسوند xml هم باید مسیریابی جدید اضافه کنی که قبلا در نظرات همین بحث مطرح شده (اولین نظر به علاوه مورد robots.txt که در نظرات بعدی هست).
PM> Install-Package DNTCommon.Web.Core