Skip to content

Commit c1433c0

Browse files
committed
updated angualr2 merge client server document
1 parent d3c82a8 commit c1433c0

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

doc/Merge-Angular-Client-Server.html

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,66 @@ <h4 id="DocStep1">Step 1: Copy files from client project to server project</h4>
1818
Copy files in below screenshot from your client solution to root folder of <strong>*Web.Host</strong> project in server solution.
1919
</p>
2020
<p>
21-
<img class="img-thumbnail" alt="copy" src="images/angular2-client-server-merge-copied-files.png"></img>
21+
<img class="img-thumbnail" alt="copy" src="images/angular2-client-server-merge-copied-files-2.png"></img>
2222
</p>
2323

24+
<h4 id="DocStep2">Step 2: Modify tsconfig.json</h4>
25+
<p>If the tsconfig.json file under src folder does not contain skipLibCheck configuration, you need to add it, because current version of lodash typings has an error.</p>
26+
<pre lang="js">
27+
"target": "es5",
28+
<strong>"skipLibCheck": true,</strong>
29+
"typeRoots": [
30+
"../node_modules/@types"
31+
]
32+
</pre>
33+
34+
<p>If the tsconfig.json file under e2e folder contains below configuration</p>
35+
<pre lang="js">
36+
"extends": "../tsconfig.json"
37+
</pre>
38+
You need to change it like this, becasue above configuration is wrong.
39+
<pre lang="js">
40+
"extends": "../src/tsconfig.json"
41+
</pre>
2442

25-
<h4 id="DocStep2">Step 2: Modify .gitignore file</h4>
43+
44+
<h4 id="DocStep3">Step 3: Modify .gitignore file</h4>
2645
<p>Since we copied .gitignore file from another project, we need to remove lines under "<strong># VS files</strong>" which contains "<strong>PhoneBook.AngularUI</strong>" keyword.</p>
2746

2847

29-
<h4 id="DocStep3">Step 3: Modify project.json</h4>
48+
<h4 id="DocStep4">Step 4: Modify *.Web.Host.csproj</h4>
3049
<p>
31-
We need to exclude "<strong>node_modules</strong>" and "<strong>external_libs</strong>" folders from compiled folders. In order to do that, change your buildOptions in project.json like this:
32-
<pre lang="js">
33-
"buildOptions": {
34-
"emitEntryPoint": true,
35-
"preserveCompilationContext": true,
36-
"copyToOutput": "log4net.config",
37-
<strong>"compile": {
38-
"exclude": [
39-
"node_modules",
40-
"external_libs"
41-
]
42-
}
43-
</strong> }
50+
We need to exclude "<strong>node_modules</strong>", "<strong>dist</strong>" and "<strong>external_libs</strong>" folders from compiled folders. In order to do that, add below content to your csproj file's content:
51+
<pre lang="xml">
52+
&lt;ItemGroup&gt;
53+
&lt;None Include="App.config" /&gt;
54+
&lt;None Update="log4net.config"&gt;
55+
&lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
56+
&lt;CopyToPublishDirectory&gt;PreserveNewest&lt;/CopyToPublishDirectory&gt;
57+
&lt;/None&gt;
58+
&lt;None Update="wwwroot\**\*"&gt;
59+
&lt;CopyToPublishDirectory&gt;PreserveNewest&lt;/CopyToPublishDirectory&gt;
60+
&lt;/None&gt;
61+
<strong> &lt;Compile Remove="dist\**" /&gt;
62+
&lt;Compile Remove="external_libs\**" /&gt;
63+
&lt;Compile Remove="node_modules\**" /&gt;
64+
&lt;EmbeddedResource Remove="dist\**" /&gt;
65+
&lt;EmbeddedResource Remove="external_libs\**" /&gt;
66+
&lt;EmbeddedResource Remove="node_modules\**" /&gt;
67+
&lt;None Remove="dist\**" /&gt;
68+
&lt;None Remove="external_libs\**" /&gt;
69+
&lt;None Remove="node_modules\**" /&gt;
70+
</strong> &lt;/ItemGroup&gt;
4471
</pre>
4572
After doing this, your project must be build successfully.
4673
</p>
4774

48-
<h4 id="DocStep4">Step 4: Running the host project</h4>
75+
<h4 id="DocStep5">Step 5: Running the host project</h4>
4976
<p>
5077
Now, we can run our host project by pressing the F5 button. We assume that, you have already applied migrations to your database and runned host project before starting this document according to <a href="Getting-Started-Angular.html">ASP.NET Core &amp; Angular 2+</a> document.
5178
</p>
5279

53-
<h4 id="DocStep5">Step 5: Running the client (Angular 2.x) project</h4>
80+
<h4 id="DocStep6">Step 6: Running the client (Angular 2.x) project</h4>
5481
<p>
5582
We need to open a command prompt and navigate to "<strong>..\src\Acme.PhoneBook.Web.Host</strong>" folder. Then we need to run "<strong>npm install</strong>" command in order to install our project's dependencies.
5683
Then we can run "<strong>npm start</strong>" command to start our project.
@@ -64,38 +91,31 @@ <h4 id="DocStep5">Step 5: Running the client (Angular 2.x) project</h4>
6491

6592

6693
<h3>Publish Configuration</h3>
67-
<h4 id="PubStep1">Step 1: Configure angular-cli.json</h4>
94+
<h4 id="PubStep1">Step 1: Configure .angular-cli.json</h4>
6895
<p>
69-
Our client project is published by angular-cli. In order to publish a single website using Visual Studio, first we need to modify publish directory for angular-cli. In order to do that, open <strong>angular-cli.json</strong> in your project and change value of <strong>"outDir"</strong> to <strong>"wwwroot/dist"</strong>.
96+
Our client project is published by angular-cli. In order to publish a single website using Visual Studio, first we need to modify publish directory for angular-cli. In order to do that, open <strong>.angular-cli.json</strong> in your project and change value of <strong>"outDir"</strong> to <strong>"wwwroot/dist"</strong>.
7097
We could use "wwwroot" as outDir but, angular-cli deletes entire folder before it builds the application. So, In order not to loose other files under
71-
<strong>wwwroot </strong>folder, we ued <strong>wwwroot/dist</strong>.
98+
<strong>wwwroot </strong>folder, we used <strong>wwwroot/dist</strong>.
7299
</p>
73100

74101
<h4 id="PubStep2">Step 2: Build client app before publish</h4>
75102
<p>
76-
Before publishing our application using Visual Studio's publish, we need to build our angular application using angular-cli. We can do it using <strong>prepublish</strong> scripts. Add <strong>"ng build --prod"</strong> to your prepublish script in project.json. Your scripts in project.json will be like this:
77-
<pre lang="js">
78-
"scripts": {
79-
"prepublish": [
80-
<strong>"ng build --prod"</strong>
81-
],
82-
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
83-
}
84-
</pre>
103+
Before publishing our application using Visual Studio's publish, we need to build our angular application using angular-cli. Your *.Web.Host.csproj will be like this:
104+
<pre lang="xml">
105+
&lt;Target Name="PrepublishScript" BeforeTargets="ComputeFilesToPublish"&gt;
106+
&lt;Exec Command="ng build --prod" /&gt;
107+
&lt;/Target&gt;</pre>
85108
In this way, angular-cli is going to build your client application before every publish you make.
86109
</p>
87110

88111
<h4 id="PubStep3">Step 3: Move client app files to wwwroot</h4>
89112
<p>
90-
We need to move files and folders under "<strong>wwwroot/dist</strong>" to "<strong>wwwroot</strong>". In order to do that, change your <strong>prepublish</strong> scripts like this:
91-
<pre lang="js">
92-
"scripts": {
93-
"prepublish": [
94-
"ng build --prod",
95-
<strong>"robocopy %publish:ProjectPath%\\wwwroot\\dist\\ %publish:ProjectPath%\\wwwroot\\ /S /E /MOVE"</strong>
96-
],
97-
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
98-
}
113+
We need to move files and folders under "<strong>wwwroot/dist</strong>" to "<strong>wwwroot</strong>". In order to do that, change your *.Web.Host.csproj like this:
114+
<pre lang="xml">
115+
&lt;Target Name="PrepublishScript" BeforeTargets="ComputeFilesToPublish"&gt;
116+
&lt;Exec Command="ng build --prod"&gt;&lt;/Exec&gt;
117+
<strong>&lt;Exec Command="robocopy $(MSBuildProjectDirectory)\wwwroot\dist\ $(MSBuildProjectDirectory)\wwwroot\ /S /E /MOVE" /&gt;</strong>
118+
&lt;/Target&gt;
99119
</pre>
100120
This command copies entire content of <strong>wwwroot\dist</strong> folder to <strong>wwwroot </strong> and deletes dist folder before publish. In this way, will have a clean wwwroot folder.
101121
</p>
@@ -119,16 +139,20 @@ <h4 id="PubStep4">Step 4: Add middleware for angular 2.x rotues</h4>
119139
});
120140

121141
</pre>
142+
<h4 id="PubStep5">Step 5: Remove HomeContoller</h4>
143+
<p>We also need to remove HomeController from our project, so the app's default route will be our angular client app. We can still access to swagger ui under
144+
<strong>/swagger/ui/index.html</strong>.</p>
122145

123146
<p>Now, we can publish our website using Visaul Studio's publish at once.</p>
124147
<h3>After Publish</h3>
125148
<p>
126149
Our client and server applications are designed to work separately. Because of that, they have different configurations. In order to make them work together, we need to make some configurations after publish as well.
127150
We need to use same address configured for our host application in <strong>appsettings.json</strong> for our angular2 application.
128-
Copy the value of "<strong>WebSiteRootAddress</strong>" in appsettings.json and use this value for "<strong>remoteServiceBaseUrl</strong>" and "<strong>appBaseUrl</strong>" in "<strong>appconfig.json</strong>" under "<strong>wwwroot\assets\</strong>" folder.
151+
First configure your appsettings.json file for values "<strong>ServerRootAddress</strong>", "<strong>ClientRootAddress</strong>", "CorsOrigins". ServerRootAddress and ClientRootAddress must be same since we are hosting client and server together. If you are using subdomain per tenancy, then you need to include allowed addresses into CorsOrigins, otherwise CorsOrigins will be same as ServerRootAddress and ClientRootAddress.
152+
Then, copy the value of "<strong>ServerRootAddress</strong>" in appsettings.json and use this value for "<strong>remoteServiceBaseUrl</strong>" and "<strong>appBaseUrl</strong>" in "<strong>appconfig.json</strong>" under "<strong>wwwroot\assets\</strong>" folder.
129153
</p>
130154
<p>
131-
Now you can view your website under "<strong>http://yourwebsite.com/index.html</strong>". If you enter only http://yourwebsite.com/, it will redirect you to swagger ui (to swagger/ui/index.html), so you need to add index.html after your website name.
155+
Now you can view your website under "<strong>http://yourwebsite.com/</strong>". If you want to view swagger ui, then you can use http://yourwebsite.com/swagger/ui/index.html.
132156
</p>
133157
<div class="alert alert-warning">
134158
Notice that, appsettings.json and appconfig.json are two different files.
Loading

0 commit comments

Comments
 (0)