Skip to content

Commit 40f56b1

Browse files
committed
add components, routing, cool navbar
1 parent 38cd884 commit 40f56b1

36 files changed

+94624
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<!-- Files not to publish (note that the 'dist' subfolders are re-added below) -->
21+
<Content Remove="ClientApp\**" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<None Remove="ClientApp\components\About.tsx" />
26+
<None Remove="ClientApp\components\Users.tsx" />
27+
<None Remove="ClientApp\empty.tsx" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<Folder Include="Areas\Admin\Controllers\" />
32+
<Folder Include="Areas\Admin\Data\" />
33+
<Folder Include="Areas\Admin\Models\" />
34+
<Folder Include="Areas\Admin\Views\" />
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<TypeScriptCompile Include="ClientApp\components\About.tsx" />
39+
<TypeScriptCompile Include="ClientApp\components\Users.tsx" />
40+
<TypeScriptCompile Include="ClientApp\empty.tsx" />
41+
</ItemGroup>
42+
43+
<Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
44+
<!-- Ensure Node.js is installed -->
45+
<Exec Command="node --version" ContinueOnError="true">
46+
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
47+
</Exec>
48+
<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." />
49+
50+
<!-- In development, the dist files won't exist on the first run or when cloning to
51+
a different machine, so rebuild them if not already present. -->
52+
<Message Importance="high" Text="Performing first-run Webpack build..." />
53+
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" />
54+
<Exec Command="node node_modules/webpack/bin/webpack.js" />
55+
</Target>
56+
57+
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
58+
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
59+
<Exec Command="npm install" />
60+
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
61+
<Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />
62+
63+
<!-- Include the newly-built files in the publish output -->
64+
<ItemGroup>
65+
<DistFiles Include="wwwroot\dist\**; ClientApp\dist\**" />
66+
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
67+
<RelativePath>%(DistFiles.Identity)</RelativePath>
68+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
69+
</ResolvedFileToPublish>
70+
</ItemGroup>
71+
</Target>
72+
73+
</Project>
Lines changed: 30 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react';
2+
3+
export class About extends React.Component {
4+
public render() {
5+
return <div>alabala</div>
6+
};
7+
}
Lines changed: 29 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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='row'>
11+
<NavMenu />
12+
{this.props.children}
13+
</div>
14+
};
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as React from 'react';
2+
import { Link, NavLink } from 'react-router-dom';
3+
import { Navbar, NavItem, Nav } from 'react-bootstrap';
4+
5+
export class NavMenu extends React.Component<{}, {}> {
6+
public render() {
7+
return <Navbar inverse collapseOnSelect>
8+
<Navbar.Header>
9+
<Navbar.Brand>
10+
<a href="#brand">ASP.NET Core with ReactJS</a>
11+
</Navbar.Brand>
12+
<Navbar.Toggle />
13+
</Navbar.Header>
14+
<Navbar.Collapse>
15+
<Nav>
16+
<NavItem eventKey={1} href="/users">Users</NavItem>
17+
<NavItem eventKey={2} href="/about">About</NavItem>
18+
</Nav>
19+
<Nav pullRight>
20+
<NavItem eventKey={1} href="#">example</NavItem>
21+
<NavItem eventKey={2} href="#">example</NavItem>
22+
</Nav>
23+
</Navbar.Collapse>
24+
</Navbar>;
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react';
2+
import { RouteComponentProps } from 'react-router';
3+
4+
interface UsersExampleState {
5+
6+
}
7+
export class Users extends React.Component<RouteComponentProps<{}>, UsersExampleState> {
8+
public render() {
9+
return <div>wait a minute</div>
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import 'components/nav-menu.css';
Lines changed: 66 additions & 0 deletions
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This is our empty TypeScript file to "lie" the compilator and everything to run smooth. :)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 { About } from './components/About';
6+
import { Users } from './components/Users';
7+
8+
export const routes = <Layout>
9+
<Route exact path='/' component={Home} />
10+
<Route path='/users' component={Users} />
11+
</Layout>;
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace ASPNETCoreReactJS_Example.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
public class SampleDataController : Controller
11+
{
12+
private static string[] Summaries = new[]
13+
{
14+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
15+
};
16+
17+
[HttpGet("[action]")]
18+
public IEnumerable<WeatherForecast> WeatherForecasts()
19+
{
20+
var rng = new Random();
21+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
22+
{
23+
DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
24+
TemperatureC = rng.Next(-20, 55),
25+
Summary = Summaries[rng.Next(Summaries.Length)]
26+
});
27+
}
28+
29+
public class WeatherForecast
30+
{
31+
public string DateFormatted { get; set; }
32+
public int TemperatureC { get; set; }
33+
public string Summary { get; set; }
34+
35+
public int TemperatureF
36+
{
37+
get
38+
{
39+
return 32 + (int)(TemperatureC / 0.5556);
40+
}
41+
}
42+
}
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ASPNETCoreReactJS_Example
2+
{
3+
using Microsoft.AspNetCore;
4+
using Microsoft.AspNetCore.Hosting;
5+
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
BuildWebHost(args).Run();
11+
}
12+
13+
public static IWebHost BuildWebHost(string[] args) =>
14+
WebHost.CreateDefaultBuilder(args)
15+
.UseStartup<Startup>()
16+
.Build();
17+
}
18+
}

0 commit comments

Comments
 (0)