This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
/
HostingEnvironmentExtensions.cs
50 lines (43 loc) · 1.89 KB
/
HostingEnvironmentExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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.IO;
using Microsoft.AspNet.FileProviders;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNet.Hosting
{
public static class HostingEnvironmentExtensions
{
private const string OldEnvironmentKey = "ASPNET_ENV";
private const string EnvironmentKey = "Hosting:Environment";
private const string WebRootKey = "webroot";
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, IConfiguration config)
{
var webRoot = config?[WebRootKey];
if (webRoot == null)
{
// Default to /wwwroot if it exists.
var wwwroot = Path.Combine(applicationBasePath, "wwwroot");
if (Directory.Exists(wwwroot))
{
hostingEnvironment.WebRootPath = wwwroot;
}
else
{
hostingEnvironment.WebRootPath = applicationBasePath;
}
}
else
{
hostingEnvironment.WebRootPath = Path.Combine(applicationBasePath, webRoot);
}
hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath);
if (!Directory.Exists(hostingEnvironment.WebRootPath))
{
Directory.CreateDirectory(hostingEnvironment.WebRootPath);
}
hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath);
var environmentName = config?[EnvironmentKey] ?? config?[OldEnvironmentKey];
hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName;
}
}
}