Take the community feedback survey now.

WebRootPath in DXP

Vote:
 

Hello,
I'm having trouble getting the WebRootPath to work in DXP, Optimizely 12.
In startup.cs im using this code and it returns an empty string

var epiWebHostingEnvironment = app.ApplicationServices.GetRequiredService<IWebHostingEnvironment>();
epiWebHostingEnvironment.WebRootPath


Same thing in a scheduled job

private readonly EPiServer.Web.IWebHostingEnvironment _env;
if(string.IsNullOrEmpty(_env.WebRootPath))
{
 return "Job terminated. WebRootPath is empty!";
}
#338045
May 08, 2025 6:22
Vote:
 

Is this an upgraded project from CMS11? In many cases those lack a wwwroot folder which is what WebRootPath will look for. In that case, either specify the path in your startup or use ContentRootPath instead.

#338047
May 08, 2025 11:53
Vote:
 

Try IWebHostEnvironment.WebRootPath (from the Microsoft.AspNetCore.Hosting namespace).

#338307
Edited, May 18, 2025 19:15
Tomas - May 20, 2025 5:49
I’ve already tried that, but it didn’t work in DXP.
- May 20, 2025 6:01
That's interesting. I have a site in DXP that use it many places. Do you have custom initialization code in Program.cs?
Vote:
 

And also, you can inject IWebHostEnvironment in the Startup constructor, capture it in a private field for use in the ConfigureServices method. Probably also in the Configure method.

#338308
May 18, 2025 19:19
Vote:
 

FYI, this is the answer I got from the support. 

As the environment at DXP is Linux OS inside a docker container, therefore, it's not possible to get a correct ContentRootPath or write files to web disk. 
So you may need to find an alternative solution, such as storing XML sitemap in the database and create a custom MVC routing to return the sitemap content programmatically.

#339813
Jul 28, 2025 9:55
Stefan Holm Olsen - Jul 28, 2025 12:09
It sounds right.
You can probably get it to work anyway. But since it is a sitemap file, which should be persistent until next update, it may be more wise to store the files as blob files (maybe in a custom content root folder) or something else.
Eric Herlitz - Aug 04, 2025 8:42
Why store the sitemap at all? Typically we generate them using a webapi, just add some memory cache to prevent request flooding.
Stefan Holm Olsen - Aug 04, 2025 15:35
Depends on the kind of site. Some huge commerce sites might not want to generate it on demand. Especially not if it gets a lot of traffic.
Vote:
 

Instead of creating your own implementation for creating XML sitemaps, why not try the following package, I have used this in many projects and it works great Geta/geta-optimizely-sitemaps: Search engine sitemaps.xml for Optimizely CMS 12 and Commerce 14

#339870
Aug 05, 2025 7:11
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.