|
1 | | -using Microsoft.AspNet.Builder; |
2 | | -using Microsoft.AspNet.Hosting; |
3 | | -using Microsoft.AspNet.NodeServices; |
4 | | -using Microsoft.AspNet.Http; |
5 | | -using Microsoft.Extensions.PlatformAbstractions; |
6 | | -using Microsoft.Extensions.Configuration; |
7 | | -using Microsoft.Extensions.DependencyInjection; |
8 | | -using Microsoft.Extensions.Logging; |
9 | | - |
10 | | -namespace ES2015Example |
11 | | -{ |
12 | | - public class Startup |
13 | | - { |
14 | | - public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) |
15 | | - { |
16 | | - // Setup configuration sources. |
17 | | - var builder = new ConfigurationBuilder() |
18 | | - .SetBasePath(appEnv.ApplicationBasePath) |
19 | | - .AddJsonFile("appsettings.json") |
20 | | - .AddEnvironmentVariables(); |
21 | | - Configuration = builder.Build(); |
22 | | - } |
23 | | - |
24 | | - public IConfigurationRoot Configuration { get; set; } |
25 | | - |
26 | | - // This method gets called by the runtime. |
27 | | - public void ConfigureServices(IServiceCollection services) |
28 | | - { |
29 | | - // Add MVC services to the services container. |
30 | | - services.AddMvc(); |
31 | | - |
32 | | - // Enable Node Services |
33 | | - services.AddNodeServices(); |
34 | | - } |
35 | | - |
36 | | - // Configure is called after ConfigureServices is called. |
37 | | - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, INodeServices nodeServices) |
38 | | - { |
39 | | - loggerFactory.MinimumLevel = LogLevel.Warning; |
40 | | - loggerFactory.AddConsole(); |
41 | | - loggerFactory.AddDebug(); |
42 | | - |
43 | | - // Configure the HTTP request pipeline. |
44 | | - |
45 | | - // Add the platform handler to the request pipeline. |
46 | | - app.UseIISPlatformHandler(); |
47 | | - |
48 | | - // Add the following to the request pipeline only in development environment. |
49 | | - if (env.IsDevelopment()) |
50 | | - { |
51 | | - app.UseDeveloperExceptionPage(); |
52 | | - } |
53 | | - else |
54 | | - { |
55 | | - // Add Error handling middleware which catches all application specific errors and |
56 | | - // send the request to the following path or controller action. |
57 | | - app.UseExceptionHandler("/Home/Error"); |
58 | | - } |
59 | | - |
60 | | - // Dynamically transpile any .js files under the '/js/' directory |
61 | | - app.Use(next => async context => { |
62 | | - var requestPath = context.Request.Path.Value; |
63 | | - if (requestPath.StartsWith("/js/") && requestPath.EndsWith(".js")) { |
64 | | - var fileInfo = env.WebRootFileProvider.GetFileInfo(requestPath); |
65 | | - if (fileInfo.Exists) { |
66 | | - var transpiled = await nodeServices.Invoke<string>("transpilation.js", fileInfo.PhysicalPath, requestPath); |
67 | | - await context.Response.WriteAsync(transpiled); |
68 | | - return; |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - // Not a JS file, or doesn't exist - let some other middleware handle it |
73 | | - await next.Invoke(context); |
74 | | - }); |
75 | | - |
76 | | - // Add static files to the request pipeline. |
77 | | - app.UseStaticFiles(); |
78 | | - |
79 | | - // Add MVC to the request pipeline. |
80 | | - app.UseMvc(routes => |
81 | | - { |
82 | | - routes.MapRoute( |
83 | | - name: "default", |
84 | | - template: "{controller}/{action?}/{id?}", |
85 | | - defaults: new { controller="Home", action = "Index" }); |
86 | | - }); |
87 | | - } |
88 | | - } |
89 | | -} |
| 1 | +using Microsoft.AspNet.Builder; |
| 2 | +using Microsoft.AspNet.Hosting; |
| 3 | +using Microsoft.AspNet.NodeServices; |
| 4 | +using Microsoft.AspNet.Http; |
| 5 | +using Microsoft.Extensions.PlatformAbstractions; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +namespace ES2015Example |
| 11 | +{ |
| 12 | + public class Startup |
| 13 | + { |
| 14 | + public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) |
| 15 | + { |
| 16 | + // Setup configuration sources. |
| 17 | + var builder = new ConfigurationBuilder() |
| 18 | + .SetBasePath(appEnv.ApplicationBasePath) |
| 19 | + .AddJsonFile("appsettings.json") |
| 20 | + .AddEnvironmentVariables(); |
| 21 | + Configuration = builder.Build(); |
| 22 | + } |
| 23 | + |
| 24 | + public IConfigurationRoot Configuration { get; set; } |
| 25 | + |
| 26 | + // This method gets called by the runtime. |
| 27 | + public void ConfigureServices(IServiceCollection services) |
| 28 | + { |
| 29 | + // Add MVC services to the services container. |
| 30 | + services.AddMvc(); |
| 31 | + |
| 32 | + // Enable Node Services |
| 33 | + services.AddNodeServices(); |
| 34 | + } |
| 35 | + |
| 36 | + // Configure is called after ConfigureServices is called. |
| 37 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, INodeServices nodeServices) |
| 38 | + { |
| 39 | + loggerFactory.MinimumLevel = LogLevel.Warning; |
| 40 | + loggerFactory.AddConsole(); |
| 41 | + loggerFactory.AddDebug(); |
| 42 | + |
| 43 | + // Configure the HTTP request pipeline. |
| 44 | + |
| 45 | + // Add the platform handler to the request pipeline. |
| 46 | + app.UseIISPlatformHandler(); |
| 47 | + |
| 48 | + // Add the following to the request pipeline only in development environment. |
| 49 | + if (env.IsDevelopment()) |
| 50 | + { |
| 51 | + app.UseDeveloperExceptionPage(); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // Add Error handling middleware which catches all application specific errors and |
| 56 | + // send the request to the following path or controller action. |
| 57 | + app.UseExceptionHandler("/Home/Error"); |
| 58 | + } |
| 59 | + |
| 60 | + // Dynamically transpile any .js files under the '/js/' directory |
| 61 | + app.Use(next => async context => { |
| 62 | + var requestPath = context.Request.Path.Value; |
| 63 | + if (requestPath.StartsWith("/js/") && requestPath.EndsWith(".js")) { |
| 64 | + var fileInfo = env.WebRootFileProvider.GetFileInfo(requestPath); |
| 65 | + if (fileInfo.Exists) { |
| 66 | + var transpiled = await nodeServices.Invoke<string>("transpilation.js", fileInfo.PhysicalPath, requestPath); |
| 67 | + await context.Response.WriteAsync(transpiled); |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Not a JS file, or doesn't exist - let some other middleware handle it |
| 73 | + await next.Invoke(context); |
| 74 | + }); |
| 75 | + |
| 76 | + // Add static files to the request pipeline. |
| 77 | + app.UseStaticFiles(); |
| 78 | + |
| 79 | + // Add MVC to the request pipeline. |
| 80 | + app.UseMvc(routes => |
| 81 | + { |
| 82 | + routes.MapRoute( |
| 83 | + name: "default", |
| 84 | + template: "{controller}/{action?}/{id?}", |
| 85 | + defaults: new { controller="Home", action = "Index" }); |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments