Files
Umbraco-CMS/src/Umbraco.Web.Common/Extensions/WebHostEnvironmentExtensions.cs
Bjarke Berg 13f6d4791c Move umbraco views to static assets and make that an RCL + Embedded language files (#12324)
* RCL for static assets to replace the nuspec

* Fix build

* Fix unit tests

* clean up in build.ps1

* Removed test (lang files will be removed later anyway)

* Fixed namespaces.. + Ensure we set web root path if missing (e.g. wwwroot folder do not exist) + Added StaticWebAssetBasePath

* fixed namespace

* cleanup

* Set root variable

* Added static assets

* Experimenting with StaticWebAssetBasePath

* Embedded lang files into Umbraco.Core

* Removed legacy test. New test can be implemented instead

* Fixed tests

* clean up

* Fix merge issue
2022-05-02 19:38:33 +02:00

46 lines
2.1 KiB
C#

using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Umbraco.Cms.Core;
namespace Umbraco.Extensions
{
/// <summary>
/// Contains extension methods for the <see cref="IWebHostEnvironment" /> interface.
/// </summary>
public static class WebHostEnvironmentExtensions
{
/// <summary>
/// Maps a virtual path to a physical path to the application's web root
/// </summary>
/// <remarks>
/// Depending on the runtime 'web root', this result can vary. For example in Net Framework the web root and the content root are the same, however
/// in netcore the web root is /wwwroot therefore this will Map to a physical path within wwwroot.
/// </remarks>
public static string MapPathWebRoot(this IWebHostEnvironment webHostEnvironment, string path)
{
var root = webHostEnvironment.WebRootPath;
//Create if missing
if (string.IsNullOrWhiteSpace(root))
{
root = webHostEnvironment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}
var newPath = path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
// TODO: This is a temporary error because we switched from IOHelper.MapPath to HostingEnvironment.MapPathXXX
// IOHelper would check if the path passed in started with the root, and not prepend the root again if it did,
// however if you are requesting a path be mapped, it should always assume the path is relative to the root, not
// absolute in the file system. This error will help us find and fix improper uses, and should be removed once
// all those uses have been found and fixed
if (newPath.StartsWith(root))
{
throw new ArgumentException("The path appears to already be fully qualified. Please remove the call to MapPathWebRoot");
}
return Path.Combine(root, newPath.TrimStart(Constants.CharArrays.TildeForwardSlashBackSlash));
}
}
}