Recently, ASP.NET team released release candidate version of Single-Page Application templates. These templates allow developers to create SPA application based on Angular, React, and React with Redux. At the time of writing this post, the final version is scheduled to release in Early 2018. This post shows how to create an Angular 5 app in 5 steps using dotnet cli.
Create an Angular 5 app in 5 steps using dotnet cli
Below are the 5 steps to create Angular 5 app using dotnet cli.
1
| dotnet new --install Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0-rc1-final |
1
| dotnet new angular |
1
| SET ASPNETCORE_Environment=Development |
1
| dotnet build |
1
| dotnet run |
That’s it. Copy the URL from command prompt and open in the browser and you should see Angular 5 app running. This is super cool and simple.
You can verify the Angular version via
package.json
file. The Client side application is placed in /ClientApp folder and package.json
file is present inside the /ClientApp folder. Below is the screenshot of the package.json
file.
All the Angular plugins are pointing to Angular 5. You can also open this application in VS Code or Visual Studio 2017. There are a couple of things to take note of.
- The
ClientApp
subdirectory is a standard Angular CLI application.So now, you can execute anyng
command (e.g., ng test), or use npm to install extra packages into it. - By default, Server-side rendering is disabled.
- In development mode, there’s no need to run
ng serve
. It runs in the background automatically, so your client-side resources are dynamically built on demand and the page refreshes when you modify any file. So when you executedotnet run
command, you notice thatng serve
command is executed.To understand this, open Startup.cs and take a look at theconfigure
method code. TheUseSpa
middleware runs the Angular CLI in development mode. - As mentioned earlier,
dotnet run
command runs the Angular compiler in the background and this increases the compile time to compile the application. Even worse is when there is a C# code change, we need to restart the application and that will also restart the Angular compiler. To speed up things, the Angular server can be run as a standalone process. You can connect to it from the ASP.NET application. To do that, navigate to ClientApp directory on command prompt and runng serve
command to start the Angular server.Copy the development server URL from the command prompt, and replace the following line in Startup.cs,1spa.UseAngularCliServer(npmScript:
"start"
);
with,1Execute thedotnet run
command and you will notice it no longer execute ng serve command in the background. - In production mode, development-time features are disabled, and your
dotnet publish
configuration automatically invokesng build
to produce minified, ahead-of-time compiled JavaScript files.
Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.