Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Trying to modify xml file stored in wwwroot - IHostingEnvironmnet it always returns null

$
0
0

I hope you are well.

I am having an issue. I want to be able to update my XML file that is stored in the web root (wwwroot) but whenever I try to use IHostingEnvironmnet it always returns null.

my code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace GWeb.Infrastructure
{
public class SiteMapHelper
{
private readonly IHostingEnvironment _hostingEnvironment;

public SiteMapHelper(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}

public void AddPostToSiteMap(int postID, string postTitle)
{
var fileName = @"sitemap.xml";
var fileLocation = _hostingEnvironment.WebRootPath;
var siteMapFilePath = Path.Combine(fileLocation, fileName);
XElement sitemapdoc = XElement.Load($"{fileName}");
var urlset = sitemapdoc.Element("urlset");
XElement url = new XElement("url",
new XElement("loc", "https://www.griffithswebdesign.com/Blog/Post/" + postID + "/" + postTitle.ToSeoUrl()),
new XElement("lastmod", DateTime.Now),
new XElement("changefreq", "weekly"),
new XElement("priority", "0.5"));
urlset.Add(url);
}


}
}

I am using asp.net core 2.1 Any suggestions that you may have will be appreciated. Thank you.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>