Skip to content

Commit e067808

Browse files
committed
Share HtmlHelper code between "legacy" ASP.NET and ASP.NET 5 assemblies
1 parent 76386b3 commit e067808

File tree

4 files changed

+32
-122
lines changed

4 files changed

+32
-122
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
2+
* Copyright (c) 2014-2015, Facebook, Inc.
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
66
* 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
*/
9-
9+
10+
#if LEGACYASPNET
11+
using System.Web;
12+
using System.Web.Mvc;
13+
using IHtmlHelper = System.Web.Mvc.HtmlHelper;
14+
#else
1015
using Microsoft.AspNet.Mvc.Rendering;
16+
using IHtmlString = Microsoft.AspNet.Mvc.Rendering.HtmlString;
17+
#endif
1118

19+
#if LEGACYASPNET
20+
namespace React.Web.Mvc
21+
#else
1222
namespace React.AspNet
23+
#endif
1324
{
1425
/// <summary>
15-
/// HTML Helpers for utilising React from an ASP.NET MVC 6 (vNext) application.
26+
/// HTML Helpers for utilising React from an ASP.NET MVC application.
1627
/// </summary>
1728
public static class HtmlHelperExtensions
18-
{
19-
// TODO: Figure out if this can be injected
29+
{
30+
2031
/// <summary>
2132
/// Gets the React environment
2233
/// </summary>
23-
private static IReactEnvironment Environment =>
24-
global::React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
34+
private static IReactEnvironment Environment
35+
{
36+
// TODO: Figure out if this can be injected
37+
get { return global::React.AssemblyRegistration.Container.Resolve<IReactEnvironment>(); }
38+
}
2539

2640
/// <summary>
2741
/// Renders the specified React component
@@ -33,7 +47,7 @@ public static class HtmlHelperExtensions
3347
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
3448
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
3549
/// <returns>The component's HTML</returns>
36-
public static HtmlString React<T>(
50+
public static IHtmlString React<T>(
3751
this IHtmlHelper htmlHelper,
3852
string componentName,
3953
T props,
@@ -62,7 +76,7 @@ public static HtmlString React<T>(
6276
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
6377
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
6478
/// <returns>The component's HTML</returns>
65-
public static HtmlString ReactWithInit<T>(
79+
public static IHtmlString ReactWithInit<T>(
6680
this IHtmlHelper htmlHelper,
6781
string componentName,
6882
T props,
@@ -88,7 +102,7 @@ public static HtmlString ReactWithInit<T>(
88102
/// attach event handlers to the server-rendered HTML.
89103
/// </summary>
90104
/// <returns>JavaScript for all components</returns>
91-
public static HtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper)
105+
public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper)
92106
{
93107
var script = Environment.GetInitJavaScript();
94108
var tag = new TagBuilder("script")

src/React.Web.Mvc3/React.Web.Mvc3.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<DebugType>full</DebugType>
1818
<Optimize>false</Optimize>
1919
<OutputPath>..\..\bin\Debug\React.Mvc3\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
20+
<DefineConstants>DEBUG;TRACE;LEGACYASPNET</DefineConstants>
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
2323
<DocumentationFile>..\..\bin\Debug\React.Mvc3\React.Web.Mvc3.XML</DocumentationFile>
@@ -28,7 +28,7 @@
2828
<DebugType>pdbonly</DebugType>
2929
<Optimize>true</Optimize>
3030
<OutputPath>..\..\bin\Release\React.Mvc3\</OutputPath>
31-
<DefineConstants>TRACE</DefineConstants>
31+
<DefineConstants>TRACE;LEGACYASPNET</DefineConstants>
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
3434
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -64,7 +64,7 @@
6464
</Reference>
6565
</ItemGroup>
6666
<ItemGroup>
67-
<Compile Include="..\React.Web.Mvc4\HtmlHelperExtensions.cs">
67+
<Compile Include="..\React.AspNet\HtmlHelperExtensions.cs">
6868
<Link>HtmlHelperExtensions.cs</Link>
6969
</Compile>
7070
<Compile Include="..\SharedAssemblyInfo.cs">

src/React.Web.Mvc4/HtmlHelperExtensions.cs

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/React.Web.Mvc4/React.Web.Mvc4.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<DebugType>full</DebugType>
1818
<Optimize>false</Optimize>
1919
<OutputPath>..\..\bin\Debug\React.Mvc4\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
20+
<DefineConstants>DEBUG;TRACE;LEGACYASPNET</DefineConstants>
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
2323
<DocumentationFile>..\..\bin\Debug\React.Mvc4\React.Web.Mvc4.XML</DocumentationFile>
@@ -28,7 +28,7 @@
2828
<DebugType>pdbonly</DebugType>
2929
<Optimize>true</Optimize>
3030
<OutputPath>..\..\bin\Release\React.Mvc4\</OutputPath>
31-
<DefineConstants>TRACE</DefineConstants>
31+
<DefineConstants>TRACE;LEGACYASPNET</DefineConstants>
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
3434
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -80,13 +80,15 @@
8080
<Reference Include="System.Xml" />
8181
</ItemGroup>
8282
<ItemGroup>
83+
<Compile Include="..\React.AspNet\HtmlHelperExtensions.cs">
84+
<Link>HtmlHelperExtensions.cs</Link>
85+
</Compile>
8386
<Compile Include="..\SharedAssemblyInfo.cs">
8487
<Link>Properties\SharedAssemblyInfo.cs</Link>
8588
</Compile>
8689
<Compile Include="..\SharedAssemblyVersionInfo.cs">
8790
<Link>Properties\SharedAssemblyVersionInfo.cs</Link>
8891
</Compile>
89-
<Compile Include="HtmlHelperExtensions.cs" />
9092
<Compile Include="Properties\AssemblyInfo.cs" />
9193
</ItemGroup>
9294
<ItemGroup>

0 commit comments

Comments
 (0)