Skip to content

Commit 38cd884

Browse files
committed
add the React template for ASP.NET Core
1 parent 1fba485 commit 38cd884

27 files changed

+4108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
6+
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<!-- Files not to publish (note that the 'dist' subfolders are re-added below) -->
20+
<Content Remove="ClientApp\**" />
21+
</ItemGroup>
22+
23+
<Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
24+
<!-- Ensure Node.js is installed -->
25+
<Exec Command="node --version" ContinueOnError="true">
26+
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
27+
</Exec>
28+
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
29+
30+
<!-- In development, the dist files won't exist on the first run or when cloning to
31+
a different machine, so rebuild them if not already present. -->
32+
<Message Importance="high" Text="Performing first-run Webpack build..." />
33+
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" />
34+
<Exec Command="node node_modules/webpack/bin/webpack.js" />
35+
</Target>
36+
37+
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
38+
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
39+
<Exec Command="npm install" />
40+
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
41+
<Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />
42+
43+
<!-- Include the newly-built files in the publish output -->
44+
<ItemGroup>
45+
<DistFiles Include="wwwroot\dist\**; ClientApp\dist\**" />
46+
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
47+
<RelativePath>%(DistFiles.Identity)</RelativePath>
48+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
49+
</ResolvedFileToPublish>
50+
</ItemGroup>
51+
</Target>
52+
53+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import './css/site.css';
2+
import 'bootstrap';
3+
import * as React from 'react';
4+
import * as ReactDOM from 'react-dom';
5+
import { AppContainer } from 'react-hot-loader';
6+
import { BrowserRouter } from 'react-router-dom';
7+
import * as RoutesModule from './routes';
8+
let routes = RoutesModule.routes;
9+
10+
function renderApp() {
11+
// This code starts up the React app when it runs in a browser. It sets up the routing
12+
// configuration and injects the app into a DOM element.
13+
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href')!;
14+
ReactDOM.render(
15+
<AppContainer>
16+
<BrowserRouter children={ routes } basename={ baseUrl } />
17+
</AppContainer>,
18+
document.getElementById('react-app')
19+
);
20+
}
21+
22+
renderApp();
23+
24+
// Allow Hot Module Replacement
25+
if (module.hot) {
26+
module.hot.accept('./routes', () => {
27+
routes = require<typeof RoutesModule>('./routes').routes;
28+
renderApp();
29+
});
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from 'react';
2+
import { RouteComponentProps } from 'react-router';
3+
4+
interface CounterState {
5+
currentCount: number;
6+
}
7+
8+
export class Counter extends React.Component<RouteComponentProps<{}>, CounterState> {
9+
constructor() {
10+
super();
11+
this.state = { currentCount: 0 };
12+
}
13+
14+
public render() {
15+
return <div>
16+
<h1>Counter</h1>
17+
18+
<p>This is a simple example of a React component.</p>
19+
20+
<p>Current count: <strong>{ this.state.currentCount }</strong></p>
21+
22+
<button onClick={ () => { this.incrementCounter() } }>Increment</button>
23+
</div>;
24+
}
25+
26+
incrementCounter() {
27+
this.setState({
28+
currentCount: this.state.currentCount + 1
29+
});
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as React from 'react';
2+
import { RouteComponentProps } from 'react-router';
3+
import 'isomorphic-fetch';
4+
5+
interface FetchDataExampleState {
6+
forecasts: WeatherForecast[];
7+
loading: boolean;
8+
}
9+
10+
export class FetchData extends React.Component<RouteComponentProps<{}>, FetchDataExampleState> {
11+
constructor() {
12+
super();
13+
this.state = { forecasts: [], loading: true };
14+
15+
fetch('api/SampleData/WeatherForecasts')
16+
.then(response => response.json() as Promise<WeatherForecast[]>)
17+
.then(data => {
18+
this.setState({ forecasts: data, loading: false });
19+
});
20+
}
21+
22+
public render() {
23+
let contents = this.state.loading
24+
? <p><em>Loading...</em></p>
25+
: FetchData.renderForecastsTable(this.state.forecasts);
26+
27+
return <div>
28+
<h1>Weather forecast</h1>
29+
<p>This component demonstrates fetching data from the server.</p>
30+
{ contents }
31+
</div>;
32+
}
33+
34+
private static renderForecastsTable(forecasts: WeatherForecast[]) {
35+
return <table className='table'>
36+
<thead>
37+
<tr>
38+
<th>Date</th>
39+
<th>Temp. (C)</th>
40+
<th>Temp. (F)</th>
41+
<th>Summary</th>
42+
</tr>
43+
</thead>
44+
<tbody>
45+
{forecasts.map(forecast =>
46+
<tr key={ forecast.dateFormatted }>
47+
<td>{ forecast.dateFormatted }</td>
48+
<td>{ forecast.temperatureC }</td>
49+
<td>{ forecast.temperatureF }</td>
50+
<td>{ forecast.summary }</td>
51+
</tr>
52+
)}
53+
</tbody>
54+
</table>;
55+
}
56+
}
57+
58+
interface WeatherForecast {
59+
dateFormatted: string;
60+
temperatureC: number;
61+
temperatureF: number;
62+
summary: string;
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from 'react';
2+
import { RouteComponentProps } from 'react-router';
3+
4+
export class Home extends React.Component<RouteComponentProps<{}>, {}> {
5+
public render() {
6+
return <div>
7+
<h1>Hello, world!</h1>
8+
<p>Welcome to your new single-page application, built with:</p>
9+
<ul>
10+
<li><a href='https://get.asp.net/'>ASP.NET Core</a> and <a href='https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx'>C#</a> for cross-platform server-side code</li>
11+
<li><a href='https://facebook.github.io/react/'>React</a> and <a href='http://www.typescriptlang.org/'>TypeScript</a> for client-side code</li>
12+
<li><a href='https://webpack.github.io/'>Webpack</a> for building and bundling client-side resources</li>
13+
<li><a href='http://getbootstrap.com/'>Bootstrap</a> for layout and styling</li>
14+
</ul>
15+
<p>To help you get started, we've also set up:</p>
16+
<ul>
17+
<li><strong>Client-side navigation</strong>. For example, click <em>Counter</em> then <em>Back</em> to return here.</li>
18+
<li><strong>Webpack dev middleware</strong>. In development mode, there's no need to run the <code>webpack</code> build tool. Your client-side resources are dynamically built on demand. Updates are available as soon as you modify any file.</li>
19+
<li><strong>Hot module replacement</strong>. In development mode, you don't even need to reload the page after making most changes. Within seconds of saving changes to files, rebuilt React components will be injected directly into your running application, preserving its live state.</li>
20+
<li><strong>Efficient production builds</strong>. In production mode, development-time features are disabled, and the <code>webpack</code> build tool produces minified static CSS and JavaScript files.</li>
21+
</ul>
22+
<h4>Going further</h4>
23+
<p>
24+
For larger applications, or for server-side prerendering (i.e., for <em>isomorphic</em> or <em>universal</em> applications), you should consider using a Flux/Redux-like architecture.
25+
You can generate an ASP.NET Core application with React and Redux using <code>dotnet new reactredux</code> instead of using this template.
26+
</p>
27+
</div>;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as React from 'react';
2+
import { NavMenu } from './NavMenu';
3+
4+
export interface LayoutProps {
5+
children?: React.ReactNode;
6+
}
7+
8+
export class Layout extends React.Component<LayoutProps, {}> {
9+
public render() {
10+
return <div className='container-fluid'>
11+
<div className='row'>
12+
<div className='col-sm-3'>
13+
<NavMenu />
14+
</div>
15+
<div className='col-sm-9'>
16+
{ this.props.children }
17+
</div>
18+
</div>
19+
</div>;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as React from 'react';
2+
import { Link, NavLink } from 'react-router-dom';
3+
4+
export class NavMenu extends React.Component<{}, {}> {
5+
public render() {
6+
return <div className='main-nav'>
7+
<div className='navbar navbar-inverse'>
8+
<div className='navbar-header'>
9+
<button type='button' className='navbar-toggle' data-toggle='collapse' data-target='.navbar-collapse'>
10+
<span className='sr-only'>Toggle navigation</span>
11+
<span className='icon-bar'></span>
12+
<span className='icon-bar'></span>
13+
<span className='icon-bar'></span>
14+
</button>
15+
<Link className='navbar-brand' to={ '/' }>ASPNETCoreReactJS_Example</Link>
16+
</div>
17+
<div className='clearfix'></div>
18+
<div className='navbar-collapse collapse'>
19+
<ul className='nav navbar-nav'>
20+
<li>
21+
<NavLink to={ '/' } exact activeClassName='active'>
22+
<span className='glyphicon glyphicon-home'></span> Home
23+
</NavLink>
24+
</li>
25+
<li>
26+
<NavLink to={ '/counter' } activeClassName='active'>
27+
<span className='glyphicon glyphicon-education'></span> Counter
28+
</NavLink>
29+
</li>
30+
<li>
31+
<NavLink to={ '/fetchdata' } activeClassName='active'>
32+
<span className='glyphicon glyphicon-th-list'></span> Fetch data
33+
</NavLink>
34+
</li>
35+
</ul>
36+
</div>
37+
</div>
38+
</div>;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.main-nav li .glyphicon {
2+
margin-right: 10px;
3+
}
4+
5+
/* Highlighting rules for nav menu items */
6+
.main-nav li a.active,
7+
.main-nav li a.active:hover,
8+
.main-nav li a.active:focus {
9+
background-color: #4189C7;
10+
color: white;
11+
}
12+
13+
/* Keep the nav menu independent of scrolling and on top of other items */
14+
.main-nav {
15+
position: fixed;
16+
top: 0;
17+
left: 0;
18+
right: 0;
19+
z-index: 1;
20+
}
21+
22+
@media (max-width: 767px) {
23+
/* On small screens, the nav menu spans the full width of the screen. Leave a space for it. */
24+
body {
25+
padding-top: 50px;
26+
}
27+
}
28+
29+
@media (min-width: 768px) {
30+
/* On small screens, convert the nav menu to a vertical sidebar */
31+
.main-nav {
32+
height: 100%;
33+
width: calc(25% - 20px);
34+
}
35+
.main-nav .navbar {
36+
border-radius: 0px;
37+
border-width: 0px;
38+
height: 100%;
39+
}
40+
.main-nav .navbar-header {
41+
float: none;
42+
}
43+
.main-nav .navbar-collapse {
44+
border-top: 1px solid #444;
45+
padding: 0px;
46+
}
47+
.main-nav .navbar ul {
48+
float: none;
49+
}
50+
.main-nav .navbar li {
51+
float: none;
52+
font-size: 15px;
53+
margin: 6px;
54+
}
55+
.main-nav .navbar li a {
56+
padding: 10px 16px;
57+
border-radius: 4px;
58+
}
59+
.main-nav .navbar a {
60+
/* If a menu item's text is too long, truncate it */
61+
width: 100%;
62+
white-space: nowrap;
63+
overflow: hidden;
64+
text-overflow: ellipsis;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from 'react';
2+
import { Route } from 'react-router-dom';
3+
import { Layout } from './components/Layout';
4+
import { Home } from './components/Home';
5+
import { FetchData } from './components/FetchData';
6+
import { Counter } from './components/Counter';
7+
8+
export const routes = <Layout>
9+
<Route exact path='/' component={ Home } />
10+
<Route path='/counter' component={ Counter } />
11+
<Route path='/fetchdata' component={ FetchData } />
12+
</Layout>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace ASPNETCoreReactJS_Example.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
public IActionResult Index()
13+
{
14+
return View();
15+
}
16+
17+
public IActionResult Error()
18+
{
19+
ViewData["RequestId"] = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
20+
return View();
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)