Skip to content

Commit e4b90c8

Browse files
authored
Add HostingEnvironment.ApplicationPhysicalPath/ApplicationVirtualPath (#491)
1 parent b3824bc commit e4b90c8

File tree

7 files changed

+53
-8
lines changed

7 files changed

+53
-8
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/HostingRuntimeExtensions.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public static void AddHostingRuntime(this IServiceCollection services)
3535
{
3636
var config = NativeMethods.HttpGetApplicationProperties();
3737

38-
options.AppDomainAppVirtualPath = config.pwzVirtualApplicationPath;
39-
options.AppDomainAppPath = config.pwzFullApplicationPath;
38+
options.ApplicationPhysicalPath = config.pwzFullApplicationPath;
39+
options.ApplicationVirtualPath = config.pwzVirtualApplicationPath;
4040
}
4141
})
4242

@@ -47,11 +47,17 @@ public static void AddHostingRuntime(this IServiceCollection services)
4747
{
4848
if (server.Features.Get<IIISEnvironmentFeature>() is { } feature)
4949
{
50-
options.AppDomainAppPath = feature.ApplicationPhysicalPath;
51-
options.AppDomainAppVirtualPath = feature.ApplicationVirtualPath;
50+
options.ApplicationPhysicalPath = feature.ApplicationPhysicalPath;
51+
options.ApplicationVirtualPath = feature.ApplicationVirtualPath;
5252
options.ApplicationID = feature.ApplicationId;
5353
options.SiteName = feature.SiteName;
5454
}
55+
})
56+
.Configure(options =>
57+
{
58+
// On ASP.NET Core this should be the same. We're doing it here rather than a PostConfigure because someone may want to set it up differently
59+
options.AppDomainAppPath = options.ApplicationPhysicalPath;
60+
options.AppDomainAppVirtualPath = options.ApplicationVirtualPath;
5561
});
5662
}
5763

src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,9 @@ namespace System.Web.Hosting
877877
public static partial class HostingEnvironment
878878
{
879879
public static string ApplicationID { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
880+
public static string ApplicationPhysicalPath { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
881+
public static string ApplicationVirtualPath { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
882+
public static System.Web.Caching.Cache Cache { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
880883
public static bool IsHosted { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
881884
public static string SiteName { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
882885
public static System.Web.Hosting.VirtualPathProvider VirtualPathProvider { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }

src/Microsoft.AspNetCore.SystemWebAdapters/Hosting/HostingEnvironment.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Web.Caching;
5+
46
namespace System.Web.Hosting;
57

68
public static class HostingEnvironment
@@ -9,6 +11,10 @@ public static class HostingEnvironment
911

1012
public static bool IsHosted => HostingEnvironmentAccessor.TryGet(out var current) && current.Options.IsHosted;
1113

14+
public static string ApplicationPhysicalPath => HostingEnvironmentAccessor.Current.Options.ApplicationPhysicalPath;
15+
16+
public static string ApplicationVirtualPath => HostingEnvironmentAccessor.Current.Options.ApplicationVirtualPath;
17+
1218
public static string SiteName => HostingEnvironmentAccessor.Current.Options.SiteName;
1319

1420
public static VirtualPathProvider? VirtualPathProvider => HostingEnvironmentAccessor.Current.Options.VirtualPathProvider;
@@ -19,4 +25,6 @@ public static void RegisterVirtualPathProvider(VirtualPathProvider provider)
1925

2026
HostingEnvironmentAccessor.Current.Options.VirtualPathProvider = provider;
2127
}
28+
29+
public static Cache Cache => HttpRuntime.Cache;
2230
}

src/Microsoft.AspNetCore.SystemWebAdapters/Security/MachineKey.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.DataProtection;
5-
using System.Web.Hosting;
65

76
namespace System.Web.Security;
87

@@ -19,7 +18,7 @@ private static IDataProtector GetProtector(string[] purposes)
1918

2019
VerifyPurposes(purposes);
2120

22-
return HostingEnvironmentAccessor.Current.Services.GetDataProtector(MachineKeyPurpose, purposes);
21+
return HttpRuntime.WebObjectActivator.GetDataProtector(MachineKeyPurpose, purposes);
2322
}
2423

2524
public static byte[] Protect(byte[] userData, params string[] purposes)

src/Microsoft.AspNetCore.SystemWebAdapters/SystemWebAdaptersOptions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if NETCOREAPP
55

66
using System;
7+
using System.Web;
78
using System.Web.Hosting;
89

910
namespace Microsoft.AspNetCore.SystemWebAdapters;
@@ -12,14 +13,39 @@ public class SystemWebAdaptersOptions
1213
{
1314
private VirtualPathProvider? _virtualPathProvider;
1415

16+
/// <summary>
17+
/// Gets or sets the value used by <see cref="HostingEnvironment.ApplicationID"/>
18+
/// </summary>
1519
public string ApplicationID { get; set; } = string.Empty;
1620

21+
/// <summary>
22+
/// Gets or sets the value used by <see cref="HostingEnvironment.IsHosted"/>
23+
/// </summary>
1724
public bool IsHosted { get; set; }
1825

26+
/// <summary>
27+
/// Gets or sets the value used by <see cref="HostingEnvironment.SiteName"/>
28+
/// </summary>
1929
public string SiteName { get; set; } = string.Empty;
2030

31+
/// <summary>
32+
/// Gets or sets the value used by <see cref="HttpRuntime.AppDomainAppVirtualPath"/>. Generally should be the same as <see cref="ApplicationVirtualPath"/> since ASP.NET Core does not have the concept of AppDomains.
33+
/// </summary>
2134
public string AppDomainAppVirtualPath { get; set; } = "/";
2235

36+
/// <summary>
37+
/// Gets or sets the value used by <see cref="HostingEnvironment.ApplicationVirtualPath"/>.
38+
/// </summary>
39+
public string ApplicationVirtualPath { get; set; } = "/";
40+
41+
/// <summary>
42+
/// Gets or sets the value used by <see cref="HostingEnvironment.ApplicationPhysicalPath"/>.
43+
/// </summary>
44+
public string ApplicationPhysicalPath { get; set; } = AppContext.BaseDirectory;
45+
46+
/// <summary>
47+
/// Gets or sets the value used by <see cref="HttpRuntime.AppDomainAppPath"/>. Generally should be the same as <see cref="ApplicationPhysicalPath"/> since ASP.NET Core does not have the concept of AppDomains.
48+
/// </summary>
2349
public string AppDomainAppPath { get; set; } = AppContext.BaseDirectory;
2450

2551
/// <summary>

src/Microsoft.AspNetCore.SystemWebAdapters/VirtualPathUtility.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using Microsoft.Extensions.DependencyInjection;
6-
using System.Web.Hosting;
76

87
namespace System.Web;
98

@@ -12,7 +11,7 @@ namespace System.Web;
1211
/// </summary>
1312
public static class VirtualPathUtility
1413
{
15-
private static VirtualPathUtilityImpl Impl => HostingEnvironmentAccessor.Current.Services.GetRequiredService<VirtualPathUtilityImpl>();
14+
private static VirtualPathUtilityImpl Impl => HttpRuntime.WebObjectActivator.GetRequiredService<VirtualPathUtilityImpl>();
1615

1716
/// <summary>Appends the literal slash mark (/) to the end of the virtual path, if one does not already exist.</summary>
1817
/// <returns>The modified virtual path.</returns>

test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/HttpRuntimeIntegrationTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public async Task ConfigureRuntimeViaConfig()
5555
// Assert
5656
Assert.Equal(IIS_SITE_NAME, options.SiteName);
5757
Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.AppDomainAppVirtualPath);
58+
Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.ApplicationVirtualPath);
5859
Assert.Equal(IIS_PHYSICAL_PATH, options.AppDomainAppPath);
60+
Assert.Equal(IIS_PHYSICAL_PATH, options.ApplicationPhysicalPath);
5961
Assert.Equal(IIS_APPLICATION_ID, options.ApplicationID);
6062
Assert.True(options.IsHosted);
6163
}
@@ -85,7 +87,9 @@ public async Task ConfigureRuntimeViaFeature()
8587
// Assert
8688
Assert.Equal(IIS_SITE_NAME, options.SiteName);
8789
Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.AppDomainAppVirtualPath);
90+
Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.ApplicationVirtualPath);
8891
Assert.Equal(IIS_PHYSICAL_PATH, options.AppDomainAppPath);
92+
Assert.Equal(IIS_PHYSICAL_PATH, options.ApplicationPhysicalPath);
8993
Assert.Equal(IIS_APPLICATION_ID, options.ApplicationID);
9094
Assert.True(options.IsHosted);
9195
}

0 commit comments

Comments
 (0)