diff --git a/doc/Merge-Angular-Client-Server.html b/doc/Merge-Angular-Client-Server.html index b812592f..62c56781 100644 --- a/doc/Merge-Angular-Client-Server.html +++ b/doc/Merge-Angular-Client-Server.html @@ -18,39 +18,66 @@

Step 1: Copy files from client project to server project

Copy files in below screenshot from your client solution to root folder of *Web.Host project in server solution.

- copy + copy

+

Step 2: Modify tsconfig.json

+

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.

+
+"target": "es5",
+"skipLibCheck": true,
+"typeRoots": [
+    "../node_modules/@types"
+]
+
+ +

If the tsconfig.json file under e2e folder contains below configuration

+
+"extends": "../tsconfig.json"
+
+You need to change it like this, becasue above configuration is wrong. +
+"extends": "../src/tsconfig.json"
+
-

Step 2: Modify .gitignore file

+ +

Step 3: Modify .gitignore file

Since we copied .gitignore file from another project, we need to remove lines under "# VS files" which contains "PhoneBook.AngularUI" keyword.

-

Step 3: Modify project.json

+

Step 4: Modify *.Web.Host.csproj

- We need to exclude "node_modules" and "external_libs" folders from compiled folders. In order to do that, change your buildOptions in project.json like this: -

-"buildOptions": {
-    "emitEntryPoint": true,
-    "preserveCompilationContext": true,
-    "copyToOutput": "log4net.config",
-    "compile": {
-      "exclude": [
-        "node_modules",
-        "external_libs"
-      ]
-    }
-  }	
+	We need to exclude "node_modules", "dist" and "external_libs" folders from compiled folders. In order to do that, add below content to your csproj file's content:
+	
+  <ItemGroup>
+    <None Include="App.config" />
+    <None Update="log4net.config">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
+    </None>
+    <None Update="wwwroot\**\*">
+      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
+    </None>
+    <Compile Remove="dist\**" />
+    <Compile Remove="external_libs\**" />
+    <Compile Remove="node_modules\**" />
+    <EmbeddedResource Remove="dist\**" />
+    <EmbeddedResource Remove="external_libs\**" />
+    <EmbeddedResource Remove="node_modules\**" />
+    <None Remove="dist\**" />
+    <None Remove="external_libs\**" />
+    <None Remove="node_modules\**" />
+  </ItemGroup>
 	
After doing this, your project must be build successfully.

-

Step 4: Running the host project

+

Step 5: Running the host project

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 ASP.NET Core & Angular 2+ document.

-

Step 5: Running the client (Angular 2.x) project

+

Step 6: Running the client (Angular 2.x) project

We need to open a command prompt and navigate to "..\src\Acme.PhoneBook.Web.Host" folder. Then we need to run "npm install" command in order to install our project's dependencies. Then we can run "npm start" command to start our project. @@ -64,38 +91,31 @@

Step 5: Running the client (Angular 2.x) project

Publish Configuration

-

Step 1: Configure angular-cli.json

+

Step 1: Configure .angular-cli.json

- 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 angular-cli.json in your project and change value of "outDir" to "wwwroot/dist". + 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 .angular-cli.json in your project and change value of "outDir" to "wwwroot/dist". 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 - wwwroot folder, we ued wwwroot/dist. + wwwroot folder, we used wwwroot/dist.

Step 2: Build client app before publish

- Before publishing our application using Visual Studio's publish, we need to build our angular application using angular-cli. We can do it using prepublish scripts. Add "ng build --prod" to your prepublish script in project.json. Your scripts in project.json will be like this: -

-"scripts": {
-    "prepublish": [
-      "ng build --prod"
-    ],
-    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
-  }	
-	
+ 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: +
+<Target Name="PrepublishScript" BeforeTargets="ComputeFilesToPublish">
+    <Exec Command="ng build --prod" />
+</Target>
In this way, angular-cli is going to build your client application before every publish you make.

Step 3: Move client app files to wwwroot

- We need to move files and folders under "wwwroot/dist" to "wwwroot". In order to do that, change your prepublish scripts like this: -

-"scripts": {
-    "prepublish": [
-      "ng build --prod",
-      "robocopy %publish:ProjectPath%\\wwwroot\\dist\\ %publish:ProjectPath%\\wwwroot\\ /S /E /MOVE"
-    ],
-    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
-  }	
+	We need to move files and folders under "wwwroot/dist" to "wwwroot". In order to do that, change your *.Web.Host.csproj like this:
+	
+<Target Name="PrepublishScript" BeforeTargets="ComputeFilesToPublish">
+    <Exec Command="ng build --prod"></Exec>
+    <Exec Command="robocopy $(MSBuildProjectDirectory)\wwwroot\dist\ $(MSBuildProjectDirectory)\wwwroot\ /S /E /MOVE" />
+</Target>
 	
This command copies entire content of wwwroot\dist folder to wwwroot and deletes dist folder before publish. In this way, will have a clean wwwroot folder.

@@ -119,16 +139,20 @@

Step 4: Add middleware for angular 2.x rotues

});
+

Step 5: Remove HomeContoller

+

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 +/swagger/ui/index.html.

Now, we can publish our website using Visaul Studio's publish at once.

After Publish

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. We need to use same address configured for our host application in appsettings.json for our angular2 application. - Copy the value of "WebSiteRootAddress" in appsettings.json and use this value for "remoteServiceBaseUrl" and "appBaseUrl" in "appconfig.json" under "wwwroot\assets\" folder. + First configure your appsettings.json file for values "ServerRootAddress", "ClientRootAddress", "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. + Then, copy the value of "ServerRootAddress" in appsettings.json and use this value for "remoteServiceBaseUrl" and "appBaseUrl" in "appconfig.json" under "wwwroot\assets\" folder.

- Now you can view your website under "http://yourwebsite.com/index.html". 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. + Now you can view your website under "http://yourwebsite.com/". If you want to view swagger ui, then you can use http://yourwebsite.com/swagger/ui/index.html.

Notice that, appsettings.json and appconfig.json are two different files. diff --git a/doc/images/angular2-client-server-merge-copied-files-2.png b/doc/images/angular2-client-server-merge-copied-files-2.png new file mode 100644 index 00000000..c2e41bec Binary files /dev/null and b/doc/images/angular2-client-server-merge-copied-files-2.png differ