forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStartup.cs
101 lines (88 loc) · 2.8 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using React.AspNet;
namespace React.Sample.CoreMvc
{
public class Startup
{
public Startup(IHostingEnvironment env, ILogger<Startup> logger)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder().AddEnvironmentVariables();
Logger = logger;
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
public ILogger<Startup> Logger { get; set; }
// This method gets called by the runtime.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// Add ReactJS.NET services.
services.AddReact();
// Build the intermediate service provider then return it
return services.BuildServiceProvider();
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
{
// Add the console logger.
loggerfactory.AddConsole();
// Add the following to the request pipeline only in development environment.
if (env.IsDevelopment())
{
//app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
// Add Error handling middleware which catches all application specific errors and
// send the request to the following path or controller action.
app.UseExceptionHandler("/Home/Error");
}
// Initialise ReactJS.NET. Must be before static files.
app.UseReact(config =>
{
config
.SetReuseJavaScriptEngines(true)
.AddScript("~/js/Sample.jsx")
.SetExceptionHandler((ex, name, id) =>
{
Logger.LogError("React component exception thrown!" + ex.ToString());
})
.SetUseDebugReact(true);
});
// Add static files to the request pipeline.
app.UseStaticFiles();
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Comments",
template: "comments/page-{page}",
defaults: new { controller = "Home", action = "Comments", page = 1 }
);
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" }
);
});
}
}
}