-
Notifications
You must be signed in to change notification settings - Fork 927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Router Support #407
Merged
Merged
React Router Support #407
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4f32fa3
Merge remote-tracking branch 'refs/remotes/reactjs/master'
gunnim 132180b
Merge remote-tracking branch 'refs/remotes/reactjs/master'
gunnim ac7fd9e
React.Router initial draft
gunnim debe9b9
spaces -> tabs
gunnim 81beb7c
tests WIP
gunnim 66cdcbd
Unit tests and final cleanup
gunnim 57f07ed
Fix htmlhelper test and comment out environmentextensionsTest
gunnim c04716e
Fix content include and only include aspnetcore files in netstandard …
gunnim e6f570d
new createComponent function added to environment interface
gunnim c2514c4
Various changes after feedback from Daniel.
gunnim 6d14da0
Finished fixing all unit tests
gunnim 5a233ec
Merge branch 'master' into master
Daniel15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<configuration> | ||
<system.web.webPages.razor> | ||
<pages> | ||
<namespaces> | ||
<add namespace="React.Router" /> | ||
</namespaces> | ||
</pages> | ||
</system.web.webPages.razor> | ||
</configuration> | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2014-Present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
namespace React.Router | ||
{ | ||
/// <summary> | ||
/// Contains the context object used during execution in addition to | ||
/// the string result of rendering the React Router component. | ||
/// </summary> | ||
public class ExecutionResult | ||
{ | ||
/// <summary> | ||
/// String result of ReactDOMServer render of provided component. | ||
/// </summary> | ||
public string RenderResult { get; set; } | ||
|
||
/// <summary> | ||
/// Context object used during JS engine execution. | ||
/// </summary> | ||
public RoutingContext Context { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2014-Present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
using System; | ||
using React.Exceptions; | ||
using React.TinyIoC; | ||
|
||
#if NET451 | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using HttpResponse = System.Web.HttpResponseBase; | ||
using IHtmlHelper = System.Web.Mvc.HtmlHelper; | ||
#else | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent; | ||
using HttpResponse = Microsoft.AspNetCore.Http.HttpResponse; | ||
using Microsoft.AspNetCore.Html; | ||
#endif | ||
|
||
namespace React.Router | ||
{ | ||
/// <summary> | ||
/// Render a React StaticRouter Component with context. | ||
/// </summary> | ||
public static class HtmlHelperExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the React environment | ||
/// </summary> | ||
private static IReactEnvironment Environment | ||
{ | ||
get | ||
{ | ||
return ReactEnvironment.GetCurrentOrThrow; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Render a React StaticRouter Component with context object. | ||
/// Can optionally be provided with a custom context handler to handle the various status codes. | ||
/// </summary> | ||
/// <param name="htmlHelper">MVC Razor <see cref="IHtmlHelper"/></param> | ||
/// <param name="componentName">Name of React Static Router component. Expose component globally to ReactJS.NET</param> | ||
/// <param name="props">Props to initialise the component with</param> | ||
/// <param name="path">F.x. from Request.Path. Used by React Static Router to determine context and routing.</param> | ||
/// <param name="contextHandler">Optional custom context handler, can be used instead of providing a Response object</param> | ||
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to <div></param> | ||
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param> | ||
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param> | ||
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param> | ||
/// <param name="containerClass">HTML class(es) to set on the container tag</param> | ||
/// <returns><see cref="IHtmlString"/> containing the rendered markup for provided React Router component</returns> | ||
public static IHtmlString ReactRouterWithContext<T>( | ||
this IHtmlHelper htmlHelper, | ||
string componentName, | ||
T props, | ||
string path = null, | ||
string htmlTag = null, | ||
string containerId = null, | ||
bool clientOnly = false, | ||
bool serverOnly = false, | ||
string containerClass = null, | ||
Action<HttpResponse, RoutingContext> contextHandler = null | ||
) | ||
{ | ||
try | ||
{ | ||
var response = htmlHelper.ViewContext.HttpContext.Response; | ||
path = path ?? htmlHelper.ViewContext.HttpContext.Request.Path; | ||
|
||
var reactComponent | ||
= Environment.CreateRouterComponent( | ||
componentName, | ||
props, | ||
path, | ||
containerId, | ||
clientOnly | ||
); | ||
|
||
if (!string.IsNullOrEmpty(htmlTag)) | ||
{ | ||
reactComponent.ContainerTag = htmlTag; | ||
} | ||
if (!string.IsNullOrEmpty(containerClass)) | ||
{ | ||
reactComponent.ContainerClass = containerClass; | ||
} | ||
|
||
var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly); | ||
|
||
if (executionResult.Context?.status != null) | ||
{ | ||
// Use provided contextHandler | ||
if (contextHandler != null) | ||
{ | ||
contextHandler(response, executionResult.Context); | ||
} | ||
// Handle routing context internally | ||
else | ||
{ | ||
SetServerResponse.ModifyResponse(executionResult.Context, response); | ||
} | ||
} | ||
|
||
return new HtmlString(executionResult.RenderResult); | ||
} | ||
finally | ||
{ | ||
Environment.ReturnEngineToPool(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
[assembly: AssemblyTitle("React.Router")] | ||
[assembly: AssemblyDescription("React Router support for ReactJS.NET")] | ||
[assembly: ComVisible(false)] | ||
[assembly: Guid("277850fc-8765-4042-945f-a50b8f2525a9")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>React Router support for ReactJS.NET.</Description> | ||
<Copyright>Copyright 2014-Present Facebook, Inc</Copyright> | ||
<AssemblyTitle>ReactJS.NET Router</AssemblyTitle> | ||
<Authors>Daniel Lo Nigro, Gunnar Már Óttarsson</Authors> | ||
<TargetFrameworks>net451;netstandard1.6</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AssemblyName>React.Router</AssemblyName> | ||
<AssemblyOriginatorKeyFile>../key.snk</AssemblyOriginatorKeyFile> | ||
<SignAssembly>true</SignAssembly> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<PackageId>React.Router</PackageId> | ||
<PackageTags>asp.net;mvc;asp;javascript;js;react;facebook;reactjs;babel;router;react router</PackageTags> | ||
<PackageIconUrl>http://reactjs.net/img/logo_64.png</PackageIconUrl> | ||
<PackageProjectUrl>http://reactjs.net/</PackageProjectUrl> | ||
<PackageLicenseUrl>https://github.com/reactjs/React.NET#licence</PackageLicenseUrl> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net451|AnyCPU'"> | ||
<DefineConstants>TRACE;DEBUG;ASPNETCORE;NET451</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\SharedAssemblyInfo.cs" /> | ||
<Compile Include="..\SharedAssemblyVersionInfo.cs" /> | ||
<Content Include="Content\**\*"> | ||
<Pack>true</Pack> | ||
<PackagePath>content\</PackagePath> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' "> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.0.3" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' "> | ||
<Reference Include="System.Web" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<PackageReference Include="Microsoft.AspNet.Mvc" Version="4.0.20710" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\React.Core\React.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The react router htmlhelper lives in this namespace. I seem to recall being unable to use it if I didn't either have this statement in my web.config or an explicit using statement in file.
You have a similar file in the mvc4 project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, interesting.