|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Builder; |
| 6 | +using Microsoft.AspNetCore.Hosting; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Newtonsoft.Json; |
| 12 | +using Newtonsoft.Json.Serialization; |
| 13 | +using React.AspNet; |
| 14 | + |
| 15 | +namespace ReactDemo |
| 16 | +{ |
| 17 | + public class Startup |
| 18 | + { |
| 19 | + public Startup(IHostingEnvironment env) |
| 20 | + { |
| 21 | + var builder = new ConfigurationBuilder() |
| 22 | + .SetBasePath(env.ContentRootPath) |
| 23 | + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
| 24 | + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) |
| 25 | + .AddEnvironmentVariables(); |
| 26 | + Configuration = builder.Build(); |
| 27 | + } |
| 28 | + |
| 29 | + public IConfigurationRoot Configuration { get; } |
| 30 | + |
| 31 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 32 | + public void ConfigureServices(IServiceCollection services) |
| 33 | + { |
| 34 | + services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |
| 35 | + services.AddReact(); |
| 36 | + // Add framework services. |
| 37 | + services.AddMvc(); |
| 38 | + } |
| 39 | + |
| 40 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 41 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
| 42 | + { |
| 43 | + loggerFactory.AddConsole(Configuration.GetSection("Logging")); |
| 44 | + loggerFactory.AddDebug(); |
| 45 | + |
| 46 | + if (env.IsDevelopment()) |
| 47 | + { |
| 48 | + app.UseDeveloperExceptionPage(); |
| 49 | + app.UseBrowserLink(); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + app.UseExceptionHandler("/Home/Error"); |
| 54 | + } |
| 55 | + |
| 56 | + // Initialise ReactJS.NET. Must be before static files. |
| 57 | + app.UseReact(config => |
| 58 | + { |
| 59 | + // If you want to use server-side rendering of React components, |
| 60 | + // add all the necessary JavaScript files here. This includes |
| 61 | + // your components as well as all of their dependencies. |
| 62 | + // See http://reactjs.net/ for more information. Example: |
| 63 | + config |
| 64 | + .AddScript("~/js/remarkable.min.js") |
| 65 | + .AddScript("~/js/tutorial.jsx") |
| 66 | + .SetJsonSerializerSettings(new JsonSerializerSettings |
| 67 | + { |
| 68 | + StringEscapeHandling = StringEscapeHandling.EscapeHtml, |
| 69 | + ContractResolver = new CamelCasePropertyNamesContractResolver() |
| 70 | + }); |
| 71 | + |
| 72 | + // If you use an external build too (for example, Babel, Webpack, |
| 73 | + // Browserify or Gulp), you can improve performance by disabling |
| 74 | + // ReactJS.NET's version of Babel and loading the pre-transpiled |
| 75 | + // scripts. Example: |
| 76 | + //config |
| 77 | + // .SetLoadBabel(false) |
| 78 | + // .AddScriptWithoutTransform("~/Scripts/bundle.server.js"); |
| 79 | + }); |
| 80 | + |
| 81 | + app.UseStaticFiles(); |
| 82 | + |
| 83 | + app.UseMvc(routes => |
| 84 | + { |
| 85 | + routes.MapRoute( |
| 86 | + name: "default", |
| 87 | + template: "{controller=Home}/{action=Index}/{id?}"); |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments