0

ASP.NET MVC 사이트 용 SiteMap을 만들고 싶습니다. 이 코드를 작성ASP.NET MVC에서 SiteMap 만들기

[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; 
       var 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 readonly object _objectToSerialize; 

     public XmlResult(object objectToSerialize) 
     { 
      _objectToSerialize = objectToSerialize; 
     } 

     public object ObjectToSerialize 
     { 
      get { return _objectToSerialize; } 
     } 

     public override void ExecuteResult(ControllerContext context) 
     { 
      if (_objectToSerialize != null) 
      { 
       context.HttpContext.Response.Clear(); 
       var xs = new XmlSerializer(_objectToSerialize.GetType()); 
       context.HttpContext.Response.ContentType = "text/xml"; 
       xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize); 
      } 
     } 
    } 

public static void RegisterRoutes(RouteCollection routes) 
     { 


      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
        "SiteMap_route", // Route name 
        "sitemap.xml", // URL with parameters 
        new { controller = "NewsFeed", action = "Sitemap", name = UrlParameter.Optional, area = "" }, // Parameter defaults 
        namespaces: new[] { "Web.Controllers" } 
      ); 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       namespaces: new[] { "Web.Controllers" } 
      ); 

     } 

처럼 사이트 맵 Sitemap

public ActionResult Sitemap() 
     { 
      var 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); 
     } 

처럼 NewsFeedController의 액션과 새로운 경로 RouteConfig 정의에서 작성하지만 입력 /sitemap.xmlerror 404

를 얻을 수

답변

2

MVC 4 이상용 MvcSiteMapProvider에 대해 동일한 문제가 발생했습니다. web.config 파일에서 UrlRoutingModule을 제거한 다음 교체하여 해결했습니다.

<configuration> 
    <system.webServer> 
    <modules> 
     <remove name="UrlRoutingModule-4.0" /> 
     <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" /> 
    </modules> 
    </system.webServer> 
</configuration> 
관련 문제