Skip to content

Commit 9cfd9c5

Browse files
isaac2004MarkPieszak
authored andcommitted
Port Project to .NET CORE 2.0/EFCore 2.0/.NET Standard 2.0 (TrilonIO#371)
Also fixed issue with Lazy Module (took + out of path) Closes TrilonIO#369
1 parent a83a6a9 commit 9cfd9c5

File tree

7 files changed

+114
-118
lines changed

7 files changed

+114
-118
lines changed

Asp2017.csproj

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp1.1</TargetFramework>
3+
<TargetFramework>netcoreapp2.0</TargetFramework>
44
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
9-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
10-
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="1.1.0" />
11-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />
14-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
15-
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0-rc3" />
8+
<!-- New Meta Package has SpaServices in It -->
9+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
10+
<PackageReference Include="NETStandard.Library" Version="2.0.0" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
1614
</ItemGroup>
1715
<ItemGroup>
1816
<!-- Files not to show in IDE -->
@@ -45,7 +43,7 @@
4543
</Target>
4644
<Target Name="CleanDist" AfterTargets="Clean">
4745
<ItemGroup>
48-
<FilesToDelete Include="Client\dist\**; wwwroot\dist\**"/>
46+
<FilesToDelete Include="Client\dist\**; wwwroot\dist\**" />
4947
</ItemGroup>
5048
<Delete Files="@(FilesToDelete)" />
5149
<RemoveDir Directories="Client\dist; wwwroot\dist" />

Client/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule, Inject } from '@angular/core';
1+
import { NgModule, Inject } from '@angular/core';
22
import { RouterModule } from '@angular/router';
33
import { CommonModule, APP_BASE_HREF } from '@angular/common';
44
import { HttpModule, Http } from '@angular/http';
@@ -135,7 +135,7 @@ export function createTranslateLoader(http: Http, baseHref) {
135135
}
136136
},
137137

138-
{ path: 'lazy', loadChildren: './containers/+lazy/lazy.module#LazyModule'},
138+
{ path: 'lazy', loadChildren: './containers/lazy/lazy.module#LazyModule'},
139139

140140
{
141141
path: '**', component: NotFoundComponent,

Server/Controllers/HomeController.cs

Lines changed: 82 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,37 @@
1212

1313
namespace AspCoreServer.Controllers
1414
{
15-
public class HomeController : Controller
15+
public class HomeController : Controller
16+
{
17+
[HttpGet]
18+
public async Task<IActionResult> Index()
1619
{
17-
[HttpGet]
18-
public async Task<IActionResult> Index()
19-
{
20-
var nodeServices = Request.HttpContext.RequestServices.GetRequiredService<INodeServices>();
21-
var hostEnv = Request.HttpContext.RequestServices.GetRequiredService<IHostingEnvironment>();
22-
23-
var applicationBasePath = hostEnv.ContentRootPath;
24-
var requestFeature = Request.HttpContext.Features.Get<IHttpRequestFeature>();
25-
var unencodedPathAndQuery = requestFeature.RawTarget;
26-
var unencodedAbsoluteUrl = $"{Request.Scheme}://{Request.Host}{unencodedPathAndQuery}";
27-
28-
// ** TransferData concept **
29-
// Here we can pass any Custom Data we want !
30-
31-
// By default we're passing down Cookies, Headers, Host from the Request object here
32-
TransferData transferData = new TransferData();
33-
transferData.request = AbstractHttpContextRequestInfo(Request);
34-
transferData.thisCameFromDotNET = "Hi Angular it's asp.net :)";
35-
// Add more customData here, add it to the TransferData class
36-
37-
// Prerender / Serialize application (with Universal)
38-
var prerenderResult = await Prerenderer.RenderToString(
20+
var nodeServices = Request.HttpContext.RequestServices.GetRequiredService<INodeServices>();
21+
var hostEnv = Request.HttpContext.RequestServices.GetRequiredService<IHostingEnvironment>();
22+
23+
var applicationBasePath = hostEnv.ContentRootPath;
24+
var requestFeature = Request.HttpContext.Features.Get<IHttpRequestFeature>();
25+
var unencodedPathAndQuery = requestFeature.RawTarget;
26+
var unencodedAbsoluteUrl = $"{Request.Scheme}://{Request.Host}{unencodedPathAndQuery}";
27+
28+
// ** TransferData concept **
29+
// Here we can pass any Custom Data we want !
30+
31+
// By default we're passing down Cookies, Headers, Host from the Request object here
32+
TransferData transferData = new TransferData();
33+
transferData.request = AbstractHttpContextRequestInfo(Request);
34+
transferData.thisCameFromDotNET = "Hi Angular it's asp.net :)";
35+
// Add more customData here, add it to the TransferData class
36+
37+
//Prerender now needs CancellationToken
38+
System.Threading.CancellationTokenSource cancelSource = new System.Threading.CancellationTokenSource();
39+
System.Threading.CancellationToken cancelToken = cancelSource.Token;
40+
41+
// Prerender / Serialize application (with Universal)
42+
var prerenderResult = await Prerenderer.RenderToString(
3943
"/",
4044
nodeServices,
45+
cancelToken,
4146
new JavaScriptModuleExport(applicationBasePath + "/Client/dist/main-server"),
4247
unencodedAbsoluteUrl,
4348
unencodedPathAndQuery,
@@ -46,66 +51,66 @@ public async Task<IActionResult> Index()
4651
Request.PathBase.ToString()
4752
);
4853

49-
ViewData["SpaHtml"] = prerenderResult.Html; // our <app> from Angular
50-
ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular
51-
ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place
52-
ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags
53-
ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags
54-
ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {};
55-
56-
return View();
57-
}
58-
59-
[HttpGet]
60-
[Route("sitemap.xml")]
61-
public async Task<IActionResult> SitemapXml()
62-
{
63-
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
64-
65-
xml += "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
66-
xml += "<sitemap>";
67-
xml += "<loc>http://localhost:4251/home</loc>";
68-
xml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>";
69-
xml += "</sitemap>";
70-
xml += "<sitemap>";
71-
xml += "<loc>http://localhost:4251/counter</loc>";
72-
xml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>";
73-
xml += "</sitemap>";
74-
xml += "</sitemapindex>";
75-
76-
return Content(xml, "text/xml");
77-
78-
}
79-
80-
public IActionResult Error()
81-
{
82-
return View();
83-
}
84-
85-
private IRequest AbstractHttpContextRequestInfo(HttpRequest request)
86-
{
87-
88-
IRequest requestSimplified = new IRequest();
89-
requestSimplified.cookies = request.Cookies;
90-
requestSimplified.headers = request.Headers;
91-
requestSimplified.host = request.Host;
92-
93-
return requestSimplified;
94-
}
54+
ViewData["SpaHtml"] = prerenderResult.Html; // our <app> from Angular
55+
ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular
56+
ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place
57+
ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags
58+
ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags
59+
ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {};
60+
61+
return View();
9562
}
9663

97-
public class IRequest
64+
[HttpGet]
65+
[Route("sitemap.xml")]
66+
public async Task<IActionResult> SitemapXml()
9867
{
99-
public object cookies { get; set; }
100-
public object headers { get; set; }
101-
public object host { get; set; }
68+
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
69+
70+
xml += "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
71+
xml += "<sitemap>";
72+
xml += "<loc>http://localhost:4251/home</loc>";
73+
xml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>";
74+
xml += "</sitemap>";
75+
xml += "<sitemap>";
76+
xml += "<loc>http://localhost:4251/counter</loc>";
77+
xml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>";
78+
xml += "</sitemap>";
79+
xml += "</sitemapindex>";
80+
81+
return Content(xml, "text/xml");
82+
83+
}
84+
85+
public IActionResult Error()
86+
{
87+
return View();
10288
}
10389

104-
public class TransferData
90+
private IRequest AbstractHttpContextRequestInfo(HttpRequest request)
10591
{
106-
public dynamic request { get; set; }
10792

108-
// Your data here ?
109-
public object thisCameFromDotNET { get; set; }
93+
IRequest requestSimplified = new IRequest();
94+
requestSimplified.cookies = request.Cookies;
95+
requestSimplified.headers = request.Headers;
96+
requestSimplified.host = request.Host;
97+
98+
return requestSimplified;
11099
}
100+
}
101+
102+
public class IRequest
103+
{
104+
public object cookies { get; set; }
105+
public object headers { get; set; }
106+
public object host { get; set; }
107+
}
108+
109+
public class TransferData
110+
{
111+
public dynamic request { get; set; }
112+
113+
// Your data here ?
114+
public object thisCameFromDotNET { get; set; }
115+
}
111116
}

Startup.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Hosting;
5-
using Microsoft.AspNetCore.Antiforgery;
6-
75
using Microsoft.AspNetCore.SpaServices.Webpack;
86
using Microsoft.Extensions.Configuration;
97
using Microsoft.Extensions.DependencyInjection;
108
using Microsoft.Extensions.Logging;
11-
using Microsoft.AspNetCore.Http;
129
using Microsoft.EntityFrameworkCore;
13-
14-
using Microsoft.AspNetCore.NodeServices;
1510
using AspCoreServer.Data;
1611
using Swashbuckle.AspNetCore.Swagger;
1712

@@ -69,26 +64,27 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6964
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
7065
loggerFactory.AddDebug();
7166

72-
app.UseStaticFiles();
67+
app.UseStaticFiles();
7368

74-
DbInitializer.Initialize(context);
69+
DbInitializer.Initialize(context);
7570

7671
if (env.IsDevelopment())
7772
{
7873
app.UseDeveloperExceptionPage();
79-
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
74+
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
75+
{
8076
HotModuleReplacement = true
8177
});
82-
8378
app.UseSwagger();
84-
85-
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
8679
app.UseSwaggerUI(c =>
8780
{
8881
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
8982
});
9083

91-
app.MapWhen(x => !x.Request.Path.Value.StartsWith("/swagger"), builder =>
84+
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
85+
86+
87+
app.MapWhen(x => !x.Request.Path.Value.StartsWith("/swagger", StringComparison.OrdinalIgnoreCase), builder =>
9288
{
9389
builder.UseMvc(routes =>
9490
{
@@ -101,19 +97,19 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
10197
else
10298
{
10399
app.UseMvc(routes =>
104-
{
105-
routes.MapRoute(
106-
name: "default",
107-
template: "{controller=Home}/{action=Index}/{id?}");
108-
109-
routes.MapRoute(
110-
"Sitemap",
111-
"sitemap.xml",
112-
new { controller = "Home", action = "SitemapXml" });
113-
114-
routes.MapSpaFallbackRoute(
115-
name: "spa-fallback",
116-
defaults: new { controller = "Home", action = "Index" });
100+
{
101+
routes.MapRoute(
102+
name: "default",
103+
template: "{controller=Home}/{action=Index}/{id?}");
104+
105+
routes.MapRoute(
106+
"Sitemap",
107+
"sitemap.xml",
108+
new { controller = "Home", action = "SitemapXml" });
109+
110+
routes.MapSpaFallbackRoute(
111+
name: "spa-fallback",
112+
defaults: new { controller = "Home", action = "Index" });
117113

118114
});
119115
app.UseExceptionHandler("/Home/Error");

global.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)