Skip to content

Commit 5bb7d50

Browse files
Fix JS engine reloading in .NET Core (#580)
* Fix JS engine reloading in .NET Core Fixes #554 * Check environment before rewriting path * Use Path.GetFullPath
1 parent 37b3f48 commit 5bb7d50

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/React.AspNet.Middleware/AspNetFileSystem.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
* Copyright (c) 2015, Facebook, Inc.
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
6+
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

@@ -13,7 +13,7 @@
1313
namespace React.AspNet
1414
{
1515
/// <summary>
16-
/// Handles file system functionality, such as reading files. Maps all paths from
16+
/// Handles file system functionality, such as reading files. Maps all paths from
1717
/// application-relative (~/...) to full paths using ASP.NET's MapPath method.
1818
/// </summary>
1919
public class AspNetFileSystem : FileSystemBase
@@ -36,12 +36,13 @@ public AspNetFileSystem(IHostingEnvironment hostingEnv)
3636
/// <returns>Full path of the file</returns>
3737
public override string MapPath(string relativePath)
3838
{
39-
if (relativePath.StartsWith(_hostingEnv.WebRootPath))
40-
{
41-
return relativePath;
42-
}
43-
relativePath = relativePath.TrimStart('~').TrimStart('/');
44-
return Path.Combine(_hostingEnv.WebRootPath, relativePath);
39+
if (relativePath.StartsWith(_hostingEnv.WebRootPath))
40+
{
41+
return relativePath;
42+
}
43+
relativePath = relativePath.TrimStart('~').TrimStart('/');
44+
45+
return Path.GetFullPath(Path.Combine(_hostingEnv.WebRootPath, relativePath));
4546
}
4647
}
4748
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#if NETCOREAPP2_0
2+
3+
using System.Runtime.InteropServices;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Moq;
6+
using React.AspNet;
7+
using Xunit;
8+
9+
namespace React.Tests.Core
10+
{
11+
public class MiddlewareTests
12+
{
13+
[Fact]
14+
public void ForwardSlashesAreTransformed()
15+
{
16+
var environment = new Mock<IHostingEnvironment>();
17+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
18+
{
19+
environment.Setup(x => x.WebRootPath).Returns("c:\\temp");
20+
Assert.Equal("c:\\temp\\wwwroot\\script.js", new AspNetFileSystem(environment.Object).MapPath("~/wwwroot/script.js"));
21+
}
22+
else
23+
{
24+
environment.Setup(x => x.WebRootPath).Returns("/var/www");
25+
Assert.Equal("/var/www/wwwroot/script.js", new AspNetFileSystem(environment.Object).MapPath("~/wwwroot/script.js"));
26+
}
27+
}
28+
}
29+
}
30+
#endif

tests/React.Tests/React.Tests.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Copyright>Copyright 2014-Present Facebook, Inc</Copyright>
@@ -30,12 +30,16 @@
3030
<ProjectReference Include="..\..\src\React.Router\React.Router.csproj" />
3131
</ItemGroup>
3232

33+
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
34+
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.0.0" />
35+
</ItemGroup>
36+
3337
<ItemGroup>
3438
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
3539
<PackageReference Include="Moq" Version="4.8.3" />
3640
<PackageReference Include="xunit" Version="2.3.1" />
3741
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
38-
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
42+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
3943
</ItemGroup>
4044

4145
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">

0 commit comments

Comments
 (0)