Skip to content

Commit

Permalink
Merge pull request #16 from fortedigital/feature/support-framework-4_8
Browse files Browse the repository at this point in the history
Added support for .NET Framework 4 8
  • Loading branch information
wredzio authored May 11, 2023
2 parents 919eee1 + 60d7acd commit a1895b6
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ FakesAssemblies/
# Visual Studio 6 workspace options file
*.opt

# Rider
.idea/

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
Expand Down
4 changes: 3 additions & 1 deletion Forte.React.AspNetCore/Configuration/ReactConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

namespace Forte.React.AspNetCore.Configuration;

internal class ReactConfiguration
public class ReactConfiguration
{
public List<string> ScriptUrls { get; set; } = new();
public bool IsServerSideDisabled { get; set; } = false;
public Version ReactVersion { get; set; } = null!;
public string NameOfObjectToSaveProps { get; set; } = "__reactProps";
public bool UseCache { get; set; } = true;
public bool StrictMode { get; set; } = false;
}
11 changes: 8 additions & 3 deletions Forte.React.AspNetCore/Forte.React.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<TargetType>library</TargetType>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Packable>true</Packable>
<VersionPrefix>0.1.2.0</VersionPrefix>
<VersionPrefix>0.2.0.0</VersionPrefix>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -33,8 +33,13 @@
<EmbeddedResource Include="renderToString.js" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="System.Web" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" />
</ItemGroup>

</Project>
8 changes: 6 additions & 2 deletions Forte.React.AspNetCore/ForteReactAspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if NET6_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -47,7 +48,7 @@ public static void AddReact(this IServiceCollection services,
}

public static void UseReact(this IApplicationBuilder app, IEnumerable<string> scriptUrls, Version reactVersion,
bool disableServerSideRendering = false, string? nameOfObjectToSaveProps = null)
bool disableServerSideRendering = false, string? nameOfObjectToSaveProps = null, bool? useCache = null, bool? strictMode = null)
{
var config = app.ApplicationServices.GetService<ReactConfiguration>();

Expand All @@ -60,5 +61,8 @@ public static void UseReact(this IApplicationBuilder app, IEnumerable<string> sc
config.ScriptUrls = scriptUrls.ToList();
config.ReactVersion = reactVersion;
config.NameOfObjectToSaveProps = nameOfObjectToSaveProps ?? config.NameOfObjectToSaveProps;
config.UseCache = useCache ?? true;
config.StrictMode = strictMode ?? false;
}
}
#endif
63 changes: 60 additions & 3 deletions Forte.React.AspNetCore/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,81 @@
using System.Threading.Tasks;
using Forte.React.AspNetCore.React;
using Forte.React.AspNetCore.React;
using Microsoft.Extensions.DependencyInjection;

#if NET48
using System.Web.Mvc;
using System.Web;
#endif

#if NET6_0_OR_GREATER
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.DependencyInjection;
#endif

namespace Forte.React.AspNetCore;

public static class HtmlHelperExtensions
{
#if NET48
public static IHtmlString React<T>(this HtmlHelper _, string componentName, T props)
{
var reactService = DependencyResolver.Current.GetService<IReactService>();
var renderedComponent = reactService.RenderToStringAsync(componentName, props).GetAwaiter().GetResult();

return new HtmlString(renderedComponent);
}

public static IHtmlString React<TComponent>(this HtmlHelper _, TComponent component) where TComponent : IReactComponent
{
var reactService = DependencyResolver.Current.GetService<IReactService>();
var renderedComponent = reactService.RenderToStringAsync(component.Path, null, component.ClientOnly).GetAwaiter().GetResult();

return new HtmlString(renderedComponent);
}

public static IHtmlString React<TComponent, TProps>(this HtmlHelper _, TComponent component) where TComponent : IReactComponent<TProps> where TProps : IReactComponentProps
{
var reactService = DependencyResolver.Current.GetService<IReactService>();
var renderedComponent = reactService.RenderToStringAsync(component.Path, component.Props, component.ClientOnly).GetAwaiter().GetResult();

return new HtmlString(renderedComponent);
}

public static IHtmlString InitJavascript(this HtmlHelper _)
{
var reactService = DependencyResolver.Current.GetService<IReactService>();

return new HtmlString(reactService.GetInitJavascript());
}
#endif

#if NET6_0_OR_GREATER
public static async Task<IHtmlContent> ReactAsync<T>(this IHtmlHelper htmlHelper, string componentName, T props)
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(await reactService.RenderToStringAsync(componentName, props));
}

public static async Task<IHtmlContent> ReactAsync<TComponent>(this IHtmlHelper htmlHelper, TComponent component) where TComponent : IReactComponent
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(await reactService.RenderToStringAsync(component.Path, null, component.ClientOnly));
}

public static async Task<IHtmlContent> ReactAsync<TComponent, TProps>(this IHtmlHelper htmlHelper, TComponent component) where TComponent : IReactComponent<TProps> where TProps : IReactComponentProps
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(await reactService.RenderToStringAsync(component.Path, component.Props, component.ClientOnly));
}

public static IHtmlContent InitJavascript(this IHtmlHelper htmlHelper)
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(reactService.GetInitJavascript());
}
#endif
}
6 changes: 2 additions & 4 deletions Forte.React.AspNetCore/JsonSerializationService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Jering.Javascript.NodeJS;
Expand All @@ -13,7 +11,7 @@ internal interface IJsonSerializationService : IJsonService
string Serialize<TValue>(TValue value);
}

internal class JsonSerializationService : IJsonSerializationService, IJsonService
internal class JsonSerializationService : IJsonSerializationService
{
private readonly JsonSerializerOptions _jsonSerializerOptions;

Expand Down
15 changes: 8 additions & 7 deletions Forte.React.AspNetCore/React/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace Forte.React.AspNetCore.React;

internal class Component
internal class Component : IReactComponent
{

public string Name { get; }
public object Props { get; }
public string Path { get; }
public object? Props { get; }
public string ContainerId { get; }
public string JsonContainerId { get; }
public bool ClientOnly { get; }

public Component(string name, object props)
public Component(string path, object? props = null, bool clientOnly = false)
{
Name = name;
Path = path;
Props = props;
ContainerId = Guid.NewGuid().ToString("n")[..8];
ClientOnly = clientOnly;
ContainerId = Guid.NewGuid().ToString("n").Substring(0, 8);
JsonContainerId = ContainerId + "-json";
}
}
12 changes: 12 additions & 0 deletions Forte.React.AspNetCore/React/IReactComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Forte.React.AspNetCore.React;

public interface IReactComponent<out TProps> : IReactComponent where TProps : IReactComponentProps
{
TProps Props { get; }
}

public interface IReactComponent
{
string Path { get; }
bool ClientOnly { get; }
}
5 changes: 5 additions & 0 deletions Forte.React.AspNetCore/React/IReactComponentProps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Forte.React.AspNetCore.React;

public interface IReactComponentProps
{
}
Loading

0 comments on commit a1895b6

Please sign in to comment.