我正在尝试在Asp.Net MVC3中创建站点地图。
Microsoft是否提供任何控制权,可以使用Razor View Engine在MVC3中创建站点地图?
如果不是,是否可以在MVC3中创建站点地图,而无需使用任何诸如Nuget MvcSiteMapProvider Package的第三方工具?
您总是可以自己动手,这是我要做的,但是这里有一些现有的实现:
https://github.com/maartenba/MvcSiteMapProvider
http://keyvan.io/sitemap-action-result-for-asp-net-mvc
我使用了类似于第二种实现的方法,并且对我来说效果很好。
[我开始为MvcSiteMapProvider捐款之前,我自己搜索了一个答案,并得出结论,答案是no。 Microsoft没有提供任何内置方法来在ASP.NET MVC中构建站点地图,搜索引擎站点地图文件或SiteMapPath控件,所有这些都由MvcSiteMapProvider提供。
sitemap是xml文件。我们可以在c#中生成站点地图。我们现在应该对此进行标记和制定规则,然后才能在c#中生成有效的站点地图。
首先创建一个站点地图索引。它是一个xml文件。您可以将其放在项目的根目录中,然后将其引入搜索引擎。这是一个有效的例子
<?xml version="1.0" encoding="ISO-8859-1"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://www.yourdomain.com/sitemap/coursesingle</loc>
<lastmod>2020-05-13T21:04:14+00:00</lastmod>
</sitemap>
<sitemap>
<loc>https://www.yourdomain.com/sitemap/videos</loc>
<lastmod>2020-05-13T21:04:14+00:00</lastmod>
</sitemap>
<sitemap>
<loc>https://www.yourdomain.com/sitemap/UserProfile</loc>
<lastmod>2020-05-13T21:04:14+00:00</lastmod>
</sitemap>
<sitemap>
<loc>https://www.yourdomain.com/sitemap/uppcategory</loc>
<lastmod>2020-05-13T21:04:14+00:00</lastmod>
</sitemap>
</sitemapindex>
将此代码添加到模型中:
public enum SitemapFrequency
{
Never,
Yearly,
Monthly,
Weekly,
Daily,
Hourly,
Always
}
这些是名称空间。将主题添加到控制器顶部:
private XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
//xmlns:video=
private XNamespace xmlnsVideo = "http://www.google.com/schemas/sitemap-video/1.1";
这是我们在sitemapindex“ coursesingle”中使用的操作
[OutputCache(Duration = 43200, Location = OutputCacheLocation.Server)]// 6 saati ast
public ContentResult coursesingle()
{
var sitemapNodes = GetSingelCourseSitemapNodes();
return getXmlSiteMapFromNodes(sitemapNodes.ToList());
}
outputcache is for performance.
GetSingelCourseSitemapNodes:
private IReadOnlyCollection<SitemapNode> GetSingelCourseSitemapNodes()//UrlHelper urlHelper
{
List<SitemapNode> nodes = new List<SitemapNode>();
var CourseNodes = db.Courses.ToList();
foreach (var item in CourseNodes)
{
SitemapFrequency frequency = SitemapFrequency.Weekly;
TimeSpan stabality = new TimeSpan();
if (item.CourseLastModified != null)
{
stabality = (DateTime)item.CourseLastModified - (DateTime)item.CoursePublishDate;
int days = (int)stabality.TotalDays;
if (days > 30 && days < 300)
frequency = SitemapFrequency.Monthly;
if (days > 300)
frequency = SitemapFrequency.Yearly;
}
else
{
}
nodes.Add(
new SitemapNode()
{
Url = Url.Action("training", "mcourse", new { id = item.CourseID, FriendlyUrl = item.CourseSlug }, "https"),
Frequency = frequency,
Priority = item.CoursePriority,
LastModified = item.CourseLastModified
});
}
return nodes;
}
在getXmlSiteMapFromNodes中,我们创建xml网站地图文件:
private ContentResult getXmlSiteMapFromNodes(List<SitemapNode> sitemapNodes)
{
XElement root = new XElement(xmlns + "urlset");
foreach (SitemapNode sitemapNode in sitemapNodes)
{
XElement urlElement = new XElement(
xmlns + "url",
new XElement(xmlns + "loc", sitemapNode.Url),
sitemapNode.LastModified == null ? null : new XElement(
xmlns + "lastmod",
MyDateTimeExtensions.ConvertDateTimeToString(sitemapNode.LastModified.Value)),
sitemapNode.Frequency == null ? null : new XElement(
xmlns + "changefreq",
sitemapNode.Frequency.Value.ToString().ToLowerInvariant()),
sitemapNode.Priority == null ? null : new XElement(
xmlns + "priority",
sitemapNode.Priority.Value.ToString("F1", CultureInfo.InvariantCulture)));
root.Add(urlElement);
}
XDocument document = new XDocument(root);
return Content("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + document.ToString(), "text/xml", Encoding.UTF8);
}