我正在尝试返回生成的.xml文件,但谷歌将其作为html页面进行选择。所以我得到:"您的站点地图似乎是一个HTML页面。请改用支持的站点地图格式。"
这是生成sitemap.xml的ASP.net控制器
[Route("sitemap.xml")]
public async Task<IActionResult> SitemapXmlAsync()
{
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("https://api.badgag.com/api/generateSitemap");
var response = await client.GetAsync("");
response.EnsureSuccessStatusCode();
var stringResult = await response.Content.ReadAsStringAsync();
var pages = JsonConvert.DeserializeObject<String[]>(stringResult);
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
xml += "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
foreach (string s in pages)
{
xml += "<sitemap>";
xml += "<loc>" + s + "</loc>";
//xml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>";
xml += "</sitemap>";
}
xml += "</sitemapindex>";
return Content(xml, "text/xml");
}
catch (HttpRequestException httpRequestException)
{
return BadRequest($"Error getting sitemap: {httpRequestException.Message}");
}
}
}
我认为我错过了一些东西。设置不同的标题?
您可以在此处查看结果:
https://badgag.com/sitemap.xml
提前致谢:)
答案 0 :(得分:0)
我找到this article about creating a XML sitemap with ASP.Net,这有助于我创建一个sitemap.ashx文件,其中包含Google和Bing所需的正确站点地图布局。
它主要是使用XmlTextWriter为站点地图生成所需的标记。此示例使用HTTPContext编写XML文件。以下是该网站的代码:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Xml;
namespace Mikesdotnetting
{
public class Sitemap : IHttpHandler
{
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/xml";
using (XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8)) {
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteStartElement("url");
string connect = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
string url = "http://www.mikesdotnetting.com/";
using (SqlConnection conn = new SqlConnection(connect)) {
using (SqlCommand cmd = new SqlCommand("GetSiteMapContent", conn)) {
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader()) {
// Get the date of the most recent article
rdr.Read();
writer.WriteElementString("loc", string.Format("{0}Default.aspx", url));
writer.WriteElementString("lastmod", string.Format("{0:yyyy-MM-dd}", rdr[0]));
writer.WriteElementString("changefreq", "weekly");
writer.WriteElementString("priority", "1.0");
writer.WriteEndElement();
// Move to the Article IDs
rdr.NextResult();
while (rdr.Read()) {
writer.WriteStartElement("url");
writer.WriteElementString("loc", string.Format("{0}Article.aspx?ArticleID={1}", url, rdr[0]));
if (rdr[1] != DBNull.Value)
writer.WriteElementString("lastmod", string.Format("{0:yyyy-MM-dd}", rdr[1]));
writer.WriteElementString("changefreq", "monthly");
writer.WriteElementString("priority", "0.5");
writer.WriteEndElement();
}
// Move to the Article Type IDs
rdr.NextResult();
while (rdr.Read()) {
writer.WriteStartElement("url");
writer.WriteElementString("loc", string.Format("{0}ArticleTypes.aspx?Type={1}", url, rdr[0]));
writer.WriteElementString("priority", "0.5");
writer.WriteEndElement();
}
// Finally move to the Category IDs
rdr.NextResult();
while (rdr.Read()) {
writer.WriteStartElement("url");
writer.WriteElementString("loc", string.Format("{0}Category.aspx?Category={1}", url, rdr[0]));
writer.WriteElementString("priority", "0.5");
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
context.Response.End();
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
}
答案 1 :(得分:-1)
试试xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string URL = @"https://badgag.com/sitemap.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(URL);
XNamespace ns = doc.Root.GetDefaultNamespace();
string[] locations = doc.Descendants(ns + "loc").Select(x => (string)x).ToArray();
}
}
}