1. Site Map Construction:
/// <Summary> /// implement site map output in XML format /// By: rhythmk.cnblogs.com /// </summary> public class SitemapResult: ActionResult {public SitemapResult (Sitemap sitemap) {this. sitemap = sitemap;} public Sitemap {get; private set;} public override void ExecuteResult (ControllerContext context) {context. httpContext. response. contentType = "text/xml"; XmlSerializer serializer = new XmlSerializer (typeof (Sitemap); serializer. serialize (context. httpContext. response. output, this. sitemap) ;}//< summary> /// site map entity /// By: rhythmk.cnblogs.com // </summary> [XmlRoot (ElementName = "urlset ", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")] [Serializable] public class Sitemap: List <SitemapUrl> {[XmlInclude (typeof (SitemapUrl)] public void Serialize (TextWriter writer) {XmlSerializer serializer = new XmlSerializer (typeof (Sitemap); XmlTextWriter xmlTextWriter = new XmlTextWriter (writer); serializer. serialize (xmlTextWriter, this) ;}[ XmlRoot (ElementName = "url")] [XmlType (TypeName = "url")] [Serializable] public class SitemapUrl {private DateTime lastModified; [XmlElement (ElementName = "loc")] public string Location {get; set;} [XmlElement (ElementName = "lastmod")] public string LastModified {get {if (DateTime. minValue. equals (this. lastModified) {this. lastModified = DateTime. now;} return this. lastModified. toString ("yyyy-MM-dd");} set {this. lastModified = DateTime. parse (value) ;}} [XmlElement (ElementName = "changefreq")] public ChangeFrequency {get; set;} [XmlElement (ElementName = "priority")] public double Priority {get; set ;}} public enum ChangeFrequency {always, hourly, daily, weekly, monthly, yearly, never}
Usage:
/// <Summary> /// site map /// </summary> /// <param name = "pageIndex"> page index </param> /// <param name = "subjectID"> subject </param> // <returns> </returns> public ActionResult Index (int subjectID = 0, int pageIndex = 1) {int total = 0; int pageSize = 2000; var list = GetExamList (subjectID, pageIndex, pageSize, ref total); Sitemap site = new Sitemap (); var time = DateTime. now. toLongDateString (); foreach (var p I N list) {site. add (new SitemapUrl () {ChangeFrequency = ChangeFrequency. weekly, LastModified = time, Location = string. format ("http: // rhythmk/home/view/{0 }? T = {1} ", p. ExaminationID, p. ExaminationTitle), Priority = 0.7}) ;}return new SitemapResult (site );}
2. Determine whether a search engine is used:
/// <Summary> /// determine whether the search engine is accessed. /// </summary> /// <param name = "useragent"> </param> /// <returns> </returns> public static bool IsSearchEngine (string useragent) {bool engine = false; if (! String. isNullOrEmpty (useragent) {var SpiderKey = System. configuration. configurationManager. appSettings ["SpiderKey"]. toString (); // spiderkey = @ "Googlebot | Feedfetcher-Google | Baiduspider | Yahoo \ s *\! \ S * Slurp | YodaoBot | Sosoimagespider | Sosospider | Sogou \ s * Web \ s * Sprider "System. text. regularExpressions. regex rx = new System. text. regularExpressions. regex (SpiderKey, RegexOptions. ignoreCase | RegexOptions. compiled); engine = rx. isMatch (useragent);} return engine;} public ActionResult View () {bool engine = IsSearchEngine (this. httpContext. request. serverVariables ["Http_User_Agent"]); // determines whether it is a search engine and uses different attempts to present data if (engine) {return View ("SearchView", entity );} else {return View (entity );}}
3. check whether it is successful:
Modify the Http_User_Agent value of the browser to simulate the search engine crawler browsing effect.
Taking Firefox as an example:
3.1 enter: about: config in the address bar and press Enter.
3.2 settings: general. useragent. override-> "Baiduspider ". It can simulate Baidu crawlers.
Default fire character: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.29 Safari/525.19
After the settings are complete, you can compare the browser page effect by browsing the http://www.docin.com/p-259119935.html this web site on another browser that is not set.