Angular CLI & ASP.NET Core WebAPI in the same project. Angular AoT compilation in development & production mode.
Get the Changelog.
- Angular v7 & ASP.NET Core 2.1
- Angular CLI
- AoT compilation in development & production mode
- Angular CLI, .NET Core CLI or Visual Studio 2017
- Angular Material
AngularCliAspNetCore
- Controllers
- ValuesController.cs Resource API
- Properties
- lanchSettings.json ASP.NET Core environments
- ClientApp Angular application
- wwwroot Root for Angular application deployment
- Startup.cs WebAPI configuration
- Requirements
- At least .NET Core 2.1
- Node.js and npm
- At least Angular CLI 7.0.0
- In ClientApp folder run:
npm install
dotnet build
- In ClientApp folder run:
npm install
- Build the solution
The app will be served on https://localhost:5001
dotnet watch run
- In ClientApp folder run:
npm run build
dotnet run --launch-profile Staging
- Select AngularCliAspNetCore profile
- Start debugging
- In ClientApp folder run:
npm run build
- Select Staging profile
- Start debugging
- Create the ASP.NET Core WebAPI:
dotnet new webapi -o AngularCliAspNetCore
- Create the Angular app:
cd AngularCliAspNetCore
ng new --skipGit=true ClientApp
- Open
angular.json
file and set theoutputPath
:
"outputPath": "../wwwroot"
- Open
package.json
file and set the following scripts:
"start": "ng serve --aot",
"build": "ng build --prod",
- Open
Startup.cs
file and add to theConfigureServices
method:
services.AddSpaStaticFiles(configuration =>
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
{
configuration.RootPath = "ClientApp/dist/ClientApp";
}
else
{
configuration.RootPath = "wwwroot";
}
});
and to Configure
method:
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
- Open launchSettings.json file and update the environments.
For other features, refer to the repository.
MIT