|
1 | 1 | ## From Scratch with Visual Studio and WebPack |
2 | 2 |
|
3 | | -__Coming Soon!__ |
| 3 | +The following steps will create a simple real-time Hello World ASP.NET Core app from Visual Studio. It will use WebPack with hot-reload feature to build the client-side code. |
| 4 | + |
| 5 | +Prerequisites: |
| 6 | + |
| 7 | +- Visual Studio 2017 |
| 8 | +- Node.js |
| 9 | + |
| 10 | +##### Create Project |
| 11 | + |
| 12 | +Create an empty ASP.NET Core Web Application (.NET Core 2.1) project and name it _HelloWorld_. Then use the NuGet Package Manager Console to install the dotNetify package: |
| 13 | +```csharp |
| 14 | +install-package DotNetify.SignalR |
| 15 | +``` |
| 16 | +<br/> |
| 17 | + |
| 18 | +##### Configure Startup |
| 19 | + |
| 20 | +Open _Startup.cs_ file and replace the content with the following: |
| 21 | +```csharp |
| 22 | +using System.IO; |
| 23 | +using System.Collections.Generic; |
| 24 | +using Microsoft.AspNetCore.Builder; |
| 25 | +using Microsoft.AspNetCore.Hosting; |
| 26 | +using Microsoft.AspNetCore.Http; |
| 27 | +using Microsoft.AspNetCore.SpaServices.Webpack; |
| 28 | +using Microsoft.Extensions.DependencyInjection; |
| 29 | +using DotNetify; |
| 30 | + |
| 31 | +namespace HelloWorld |
| 32 | +{ |
| 33 | + public class Startup |
| 34 | + { |
| 35 | + public void ConfigureServices(IServiceCollection services) |
| 36 | + { |
| 37 | + services.AddMemoryCache(); |
| 38 | + services.AddSignalR(); |
| 39 | + services.AddDotNetify(); |
| 40 | + } |
| 41 | + |
| 42 | + public void Configure(IApplicationBuilder app) |
| 43 | + { |
| 44 | + app.UseWebSockets(); |
| 45 | + app.UseSignalR(routes => routes.MapDotNetifyHub()); |
| 46 | + app.UseDotNetify(); |
| 47 | + |
| 48 | + // Optional: utilize webpack hot reload feature. |
| 49 | + app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions |
| 50 | + { |
| 51 | + HotModuleReplacement = true, |
| 52 | + HotModuleReplacementClientOptions = new Dictionary<string, string> { { "reload", "true" } }, |
| 53 | + }); |
| 54 | + |
| 55 | + app.UseStaticFiles(); |
| 56 | + app.Run(async (context) => |
| 57 | + { |
| 58 | + using (var reader = new StreamReader(File.OpenRead("wwwroot/index.html"))) |
| 59 | + await context.Response.WriteAsync(reader.ReadToEnd()); |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | +<br/> |
| 66 | + |
| 67 | +##### Configure NPM |
| 68 | + |
| 69 | +Add NPM configuration file _package.json_ with the following content: |
| 70 | +```json |
| 71 | +{ |
| 72 | + "name": "helloworld", |
| 73 | + "scripts": { |
| 74 | + "build": "webpack" |
| 75 | + }, |
| 76 | + "dependencies": { |
| 77 | + "dotnetify": "~3.2.0", |
| 78 | + "vue": "~2.5.17" |
| 79 | + }, |
| 80 | + "devDependencies": { |
| 81 | + "aspnet-webpack": "^3.0.0", |
| 82 | + "vue-loader": "~15.4.2", |
| 83 | + "vue-template-compiler": "~2.5.17", |
| 84 | + "webpack": "^4.18.0", |
| 85 | + "webpack-cli": "^3.1.0", |
| 86 | + "webpack-dev-middleware": "^3.3.0", |
| 87 | + "webpack-hot-middleware": "^2.23.1" |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +The packages will be automatically downloaded by Visual Studio when the file is saved. |
| 93 | +<br/> |
| 94 | + |
| 95 | +##### Configure WebPack |
| 96 | + |
| 97 | +Add _webpack.config.js_ with the following content: |
| 98 | +```js |
| 99 | +'use strict'; |
| 100 | + |
| 101 | +const { VueLoaderPlugin } = require('vue-loader'); |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + mode: 'development', |
| 105 | + entry: { main: './src/index.js' }, |
| 106 | + output: { |
| 107 | + path: __dirname + '/wwwroot/dist', |
| 108 | + publicPath: '/dist/' |
| 109 | + }, |
| 110 | + resolve: { |
| 111 | + modules: [ 'src', 'node_modules' ], |
| 112 | + alias: { vue$: 'vue/dist/vue.esm.js' } |
| 113 | + }, |
| 114 | + module: { |
| 115 | + rules: [ { test: /\.vue$/, use: 'vue-loader' } ] |
| 116 | + }, |
| 117 | + plugins: [ new VueLoaderPlugin() ] |
| 118 | +``` |
| 119 | +<br/> |
| 120 | +
|
| 121 | +##### Add Index Page |
| 122 | +
|
| 123 | +Add a new file _wwwroot/index.html_ with the following content: |
| 124 | +```html |
| 125 | +<html> |
| 126 | + <head> |
| 127 | + <title>DotNetify</title> |
| 128 | + <meta charset="utf-8"> |
| 129 | + <meta name="viewport" content="initial-scale=1, width=device-width" /> |
| 130 | + </head> |
| 131 | + <body> |
| 132 | + <div id="App"></div> |
| 133 | + <script src="dist/main.js" charset="UTF-8"></script> |
| 134 | + </body> |
| 135 | +</html> |
| 136 | +``` |
| 137 | +<br/> |
| 138 | +
|
| 139 | +##### Add Hello World |
| 140 | +
|
| 141 | +Create a new folder _src_, and add a new file _src/index.js_ with the following content: |
| 142 | +```jsx |
| 143 | +import Vue from 'vue'; |
| 144 | +import HelloWorld from './HelloWorld.vue'; |
| 145 | + |
| 146 | +document.getElementById('App').innerHTML = '<hello-world />'; |
| 147 | +new Vue({ |
| 148 | + el: '#App', |
| 149 | + components: { |
| 150 | + 'hello-world': HelloWorld |
| 151 | + } |
| 152 | +}); |
| 153 | +``` |
| 154 | +
|
| 155 | +Add a new file _src/HelloWorld.vue_ with the following content: |
| 156 | +```html |
| 157 | +<template> |
| 158 | + <div> |
| 159 | + <p>{{Greetings}}</p> |
| 160 | + <p>Server time is: {{ServerTime}}</p> |
| 161 | + </div> |
| 162 | +</template> |
| 163 | + |
| 164 | +<script> |
| 165 | +import dotnetify from 'dotnetify/vue'; |
| 166 | + |
| 167 | +export default { |
| 168 | + name: 'HelloWorld', |
| 169 | + created: function () { |
| 170 | + this.vm = dotnetify.vue.connect("HelloWorld", this); |
| 171 | + }, |
| 172 | + destroyed: function () { |
| 173 | + this.vm.$destroy(); |
| 174 | + }, |
| 175 | + data: function () { |
| 176 | + return { Greetings: '', ServerTime: '' } |
| 177 | + } |
| 178 | +} |
| 179 | +</script> |
| 180 | +``` |
| 181 | +
|
| 182 | +Add a new file _HelloWorld.cs_ with the following content: |
| 183 | +```csharp |
| 184 | +using System; |
| 185 | +using DotNetify; |
| 186 | +using System.Threading; |
| 187 | + |
| 188 | +namespace HelloWorld |
| 189 | +{ |
| 190 | + public class HelloWorld : BaseVM |
| 191 | + { |
| 192 | + private Timer _timer; |
| 193 | + public string Greetings => "Hello World!"; |
| 194 | + public DateTime ServerTime => DateTime.Now; |
| 195 | + |
| 196 | + public HelloWorld() |
| 197 | + { |
| 198 | + _timer = new Timer(state => |
| 199 | + { |
| 200 | + Changed(nameof(ServerTime)); |
| 201 | + PushUpdates(); |
| 202 | + }, null, 0, 1000); |
| 203 | + } |
| 204 | + |
| 205 | + public override void Dispose() => _timer.Dispose(); |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | +<br/> |
| 210 | +
|
| 211 | +##### Build and Run |
| 212 | +
|
| 213 | +Run the application. Hello World! |
0 commit comments