why the "angular part" of abp .netcore template is distributed in a visual studio solution (AbpCore.AngularUI.sln)? Is there any .netcore code inside it or some serverside functionality?
is the whole serverside code in the other solution (AbpCore.sln), or there is more?
In enterprise companies front-end and back-end developer teams are separated. So client-side and server-side code structure is seperated. But if you want you can merge the solutions following the steps below.
Copy files in below screenshot from your client solution to root folder of *Web.Host project in server solution.
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"
Since we copied .gitignore file from another project, we need to remove lines under "# VS files" which contains "PhoneBook.AngularUI" keyword.
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.
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.
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.
After this point, we can work on a single Visual Studio 2017+ solution with our client and server projects. Since we use angular-cli for serving our client application, we cannot use same port both for our client and server apps during development. But, we can do it when publishing our application. Let's see how.
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 used wwwroot/dist.
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.
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" IgnoreExitCode="True" />
<ItemGroup>
<DistFiles Include="wwwroot\**\*" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</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.
We need to do one more thing before publising our application. Since we host our Angular 2.x application in a ASP.NET Core website, we will have a routing problem. After publishing our application, if we refresh page when we navigate to a page in our application (for example: /admin/users), we will have a empty page. Because when we request an url like http://yourwebsite.com/admin/users, ASP.NET Core will not be able to find a matching Controller. In order to overcome this problem, add below code to Startup.cs file just before "app.UseStaticFiles();" line.
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404
&& !Path.HasExtension(context.Request.Path.Value))
{
context.Request.Path = "/index.html";
await next();
}
});
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. 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/". 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.
Originally, you can check out asp.net zero merging angular client codes documentation