Skip to content

Commit

Permalink
Add sample for react router
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinsoftware committed Nov 23, 2017
1 parent 48bd132 commit 0e9f5bf
Show file tree
Hide file tree
Showing 16 changed files with 3,197 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/React.Sample.Router.CoreMvc/.babelrc
@@ -0,0 +1,6 @@
{
"presets": ["react", "env"],
"plugins": [
"add-module-exports"
]
}
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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>
60 changes: 60 additions & 0 deletions src/React.Sample.Router.CoreMvc/Startup.cs
@@ -0,0 +1,60 @@
using System;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Msie;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
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();

// 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");
JsEngineSwitcher.Instance.EngineFactories.Add(new MsieJsEngineFactory());
JsEngineSwitcher.Instance.DefaultEngineName = MsieJsEngine.EngineName;
});

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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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'
}
]
}
};

0 comments on commit 0e9f5bf

Please sign in to comment.