Skip to content
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

Add react router sample #479

Merged
merged 2 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/React.Router/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ var reactComponent

var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly);

if (executionResult.Context?.status != null)
if (executionResult.Context?.status != null || executionResult.Context?.url != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible for a redirect to be returned without a status code defined

{
// Use provided contextHandler
if (contextHandler != null)
Expand Down
2 changes: 1 addition & 1 deletion src/React.Router/SetServerResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class SetServerResponse
/// <param name="Response">The response object to use.</param>
public static void ModifyResponse(RoutingContext context, HttpResponse Response)
{
var statusCode = context.status.Value;
var statusCode = context.status ?? 302;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a status code is omitted we can't know if the author intended a permanent or temporary redirect, therefore it's safest to assume temporary


// 300-399
if (statusCode >= 300 && statusCode < 400)
Expand Down
6 changes: 6 additions & 0 deletions src/React.Sample.Router.CoreMvc/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": ["react", "env"],
"plugins": [
"add-module-exports"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('expose-loader?React!react');
require('expose-loader?ReactDOM!react-dom');
require('expose-loader?ReactDOMServer!react-dom/server');

require('expose-loader?RootComponent!./home.jsx');
78 changes: 78 additions & 0 deletions src/React.Sample.Router.CoreMvc/Content/components/home.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Component } from 'react';
import {
Link,
BrowserRouter,
Route,
Switch,
StaticRouter,
Redirect
} from 'react-router-dom';

class Navbar extends Component {
render() {
return (
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/contact">Contact</Link>
</li>
</ul>
);
}
}

class HomePage extends Component {
render() {
return <h1>Home</h1>;
}
}

class AboutPage extends Component {
render() {
return <h1>About</h1>;
}
}

class ContactPage extends Component {
render() {
return <h1>Contact</h1>;
}
}

export default class HomeComponent extends Component {
render() {
const app = (
<div>
<Navbar />
<Switch>
<Route exact path="/" render={() => <Redirect to="/home" />} />
<Route path="/home" component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route path="/contact" component={ContactPage} />
<Route
path="*"
component={({ staticContext }) => {
if (staticContext) staticContext.status = 404;

return <h1>Not Found :(</h1>;
}}
/>
</Switch>
</div>
);

if (typeof window === 'undefined') {
return (
<StaticRouter context={this.props.context} location={this.props.path}>
{app}
</StaticRouter>
);
}
return <BrowserRouter>{app}</BrowserRouter>;
}
}
12 changes: 12 additions & 0 deletions src/React.Sample.Router.CoreMvc/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace React.Sample.Router.CoreMvc.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
18 changes: 18 additions & 0 deletions src/React.Sample.Router.CoreMvc/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace React.Sample.Router.CoreMvc
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
29 changes: 29 additions & 0 deletions src/React.Sample.Router.CoreMvc/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:9456/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"React.Sample.Router.CoreMvc": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:9457/"
}
}
}
21 changes: 21 additions & 0 deletions src/React.Sample.Router.CoreMvc/React.Sample.Router.CoreMvc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\React.AspNet.Middleware\React.AspNet.Middleware.csproj" />
<ProjectReference Include="..\React.AspNet\React.AspNet.csproj" />
<ProjectReference Include="..\React.Core\React.Core.csproj" />
<ProjectReference Include="..\React.Router\React.Router.csproj" />
</ItemGroup>
</Project>
59 changes: 59 additions & 0 deletions src/React.Sample.Router.CoreMvc/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Msie;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using React.AspNet;

namespace React.Sample.Router.CoreMvc
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddReact();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

// Build the intermediate service provider then return it
return services.BuildServiceProvider();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();

// Initialise ReactJS.NET. Must be before static files.
app.UseReact(config =>
{
config
.SetReuseJavaScriptEngines(true)
.SetLoadBabel(false)
.SetLoadReact(false)
.AddScriptWithoutTransform("~/components-bundle.generated.js");
});

app.UseMvc(routes =>
{
routes.MapRoute("default", "{path?}", new { controller = "Home", action = "Index" });
});
}
}
}
15 changes: 15 additions & 0 deletions src/React.Sample.Router.CoreMvc/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@using React.AspNet
@using React.Router

<!DOCTYPE html>
<html>
<head>
<title>React Router Sample</title>
<meta charset="utf-8" />
</head>
<body>
@Html.ReactRouterWithContext("RootComponent", new { })
<script src="/components-bundle.generated.js"></script>
@Html.ReactInitJavaScript()
</body>
</html>
10 changes: 10 additions & 0 deletions src/React.Sample.Router.CoreMvc/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
15 changes: 15 additions & 0 deletions src/React.Sample.Router.CoreMvc/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
21 changes: 21 additions & 0 deletions src/React.Sample.Router.CoreMvc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "React.Sample.Router.CoreMvc",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "webpack"
},
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"expose-loader": "^0.7.3",
"react": "^16.1.1",
"react-dom": "^16.1.1",
"react-router-dom": "^4.2.2",
"webpack": "^3.8.1"
}
}
15 changes: 15 additions & 0 deletions src/React.Sample.Router.CoreMvc/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
entry: './Content/components/expose-components.js',
output: {
filename: './wwwroot/components-bundle.generated.js'
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
}
};
Loading