// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
namespace Microsoft.AspNet.Hosting
{
///
/// Extension methods for .
///
public static class HostingEnvironmentExtensions
{
///
/// Checks if the current hosting environment name is "Development".
///
/// An instance of .
/// True if the environment name is "Development", otherwise false.
public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return hostingEnvironment.IsEnvironment(EnvironmentName.Development);
}
///
/// Checks if the current hosting environment name is "Staging".
///
/// An instance of .
/// True if the environment name is "Staging", otherwise false.
public static bool IsStaging(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return hostingEnvironment.IsEnvironment(EnvironmentName.Staging);
}
///
/// Checks if the current hosting environment name is "Production".
///
/// An instance of .
/// True if the environment name is "Production", otherwise false.
public static bool IsProduction(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return hostingEnvironment.IsEnvironment(EnvironmentName.Production);
}
///
/// Compares the current hosting environment name against the specified value.
///
/// An instance of .
/// Environment name to validate against.
/// True if the specified name is the same as the current environment, otherwise false.
public static bool IsEnvironment(
this IHostingEnvironment hostingEnvironment,
string environmentName)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return string.Equals(
hostingEnvironment.EnvironmentName,
environmentName,
StringComparison.OrdinalIgnoreCase);
}
///
/// Determines the physical path corresponding to the given virtual path.
///
/// An instance of .
/// Path relative to the application root.
/// Physical path corresponding to the virtual path.
public static string MapPath(
this IHostingEnvironment hostingEnvironment,
string virtualPath)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
if (string.IsNullOrEmpty(hostingEnvironment.WebRootPath))
{
throw new InvalidOperationException("Can not map path because webroot path is not set");
}
if (virtualPath == null)
{
return hostingEnvironment.WebRootPath;
}
// On windows replace / with \.
virtualPath = virtualPath.Replace('/', Path.DirectorySeparatorChar);
return Path.Combine(hostingEnvironment.WebRootPath, virtualPath);
}
}
}