-
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
Add react router sample #479
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this change needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
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'); |
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>; | ||
} | ||
} |
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(); | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} |
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/" | ||
} | ||
} | ||
} |
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> |
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" }); | ||
}); | ||
} | ||
} | ||
} |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
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" | ||
} | ||
} | ||
} | ||
} |
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" | ||
} | ||
} |
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' | ||
} | ||
] | ||
} | ||
}; |
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.
Why is this change 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.
It is possible for a redirect to be returned without a status code defined