Create an application with Angular 6 and .Net Core: Step by step guide

ang14

Angular 6 is announced recently which has introduced some pretty awesome features.

Let us see how to create Angular 6 application with and without using .Net Core SPA templates using Visual Studio 2017.

There are more than one ways to create Angular 6 application with .Net Core, Let us see some of them.

Make sure you have instilled Visual Studio 2017 & .Net Core latest SDK and of course Node & Angular CLI.

Without using SPA Template

In this approach we will not use any template and will add Angular 6 within an API project.

Create the Angular application using .Net Core 2.0 template in VS 2017

Once you have all these installed, open your Visual Studio 2017 -> Create New Project -> Select Core Web application:

ang9

Click on Ok and in next window, select API as shown below:

ang10

Once the API project is created, open Command prompt and navigate to the project folder -> run command:

ng new ClientApp

This will create Angular 6 application within API project.

Next task is to run our Angular application with .Net Core. For this we need to add some code within Startup.cs class.

Add below lines in ConfigureService method:


services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});

Add below lines in Configure method:


app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();

app.UseHttpsRedirection();
app.UseMvc();

app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});

Above code adds required configuration for running Angular code with .Net Core.

Note: Make sure the property “launchUrl”: “api/values” is not present in the Properties/launchSettings.json, if it is available then delete it.

Just run the application in the browser:

ang11

Your Angular 6 application is now running with .Net Core.

With SPA Template

In first approach, we have not used SPA template to create Angular app, in this approach we will use the Angular template.

Create the Angular application using .Net Core 2.1 template in VS 2017

Once you have all these installed, open your Visual Studio 2017 -> Create New Project -> Select Core Web application:

ang1

Click on Ok and in next window, select Angular as shown below:

ang2

Visual Studio will create a well-structured application for you.

If you compare latest project structure with the previous versions then you would notice that the Views folder is no more there:

ang3

We do not need that Views folder now.

Delete ClientApp and install Angular 6

If you open package.json file under ClientApp folder, you would notice the Angular version is 5.0 but we want to create Angular 6 application.

So go to File explorer and delete ClientApp folder:

ang4

Once the folder is deleted, open Command prompt and navigate to the project and run command:

ng new ClientApp

This command will create brand new Angular application with latest version.

ang5

Once the process is completed, go back to solution explorer -> ClientApp -> package.json file, it should show Angular 6.0 references:

ang6

That is it. We have just created Angular 6 application with .Net Core.

Let us try to use any random Angular 6 feature just to make sure we have latest Angular 6 code.

Test Library feature

We will test the library feature which has been shipped with Angular 6.

Open the command prompt again and navigate to ClientApp folder -> run command:

ng generate library my-shared-library

ang7

Logs in the window confirms that we have Angular 6 now. Library would be created under the ClientApp -> Projects folder:

ang8

 

Hope it helps.

 

 

 

Advertisements

38 thoughts on “Create an application with Angular 6 and .Net Core: Step by step guide

  1. Neel:

    Considering 2.1’s integration with Angular 6, would you say the best practice for UI in .Net Core is to use Angular as your front end rather than Razor files?

    Thanks,

    mark

    Like

    1. Hi Mark, from .Net Core 2.1 on wards, default template does not include the views which means it would be good to use Angular as front end but we can use Razor as well. It makes more sense to use Angular though.

      Like

  2. Neel:

    I hate to ask a dumb question, but could you please specify how to “run the app”. I open a command prompt in the project folder, and run ng serve. I receive the error “Local workspace file (‘angular.json’) could not be found” What am I doing wrong?

    Thanks in advance.

    Like

      1. Is “ng serve” the accepted way to run/debug the application when using visual studio? If I try to run the app in IIS I get errors about paths to NPM not found, but using “ng serve” from command prompt works.

        Like

      2. Hi, ng serve is basically used to test your app locally while developing. While developing, you can use ng serve which will build the application and start a web server where you can test. Deploying to IIS is a different thing because you are using IIS server for that. Have a look here for hosting Angular apps on IIS: https://www.google.co.in/search?safe=strict&ei=ngBkW9DaJYn28gWgo5_YAw&q=host+angular+app+in+iis&oq=host+angular+app&gs_l=psy-ab.1.0.0i20i263k1j0l9.2144.5919.0.7918.18.14.1.3.3.0.289.1936.0j4j5.9.0….0…1c.1.64.psy-ab..6.12.1665…35i39k1j0i67k1j0i10k1j0i131k1j0i22i30k1.0.7avk2QHyTw8

        Like

  3. Neel, first of all, great post!!

    But trying to expand Mark’s question… What would you consider to be the pros/cons of choosing Angular over Razor UI? What would be de main differences? Thank you!!

    Like

  4. One thing to mention is that you need to change parameter in angular.json to make it work in Production (in Azure):
    “outputPath”: “dist”
    instead of
    “outputPath”: “dist/ClientApp”
    to match original .angular-cli.json setting

    Like

  5. Thank you Neel, This is really good post to start on AngularJS with .Net Core.
    While running the application, I was facing weird issue. Initially was getting HTTP ERROR 500.

    This page isn’t working
    localhost is currently unable to handle this request.
    HTTP ERROR 500

    But, after waiting for some time when I reload the browser, it’s working fine.

    Any guess ?

    Like

    1. An unhandled exception occurred while processing the request.
      TimeoutException: The Angular CLI process did not start listening for requests within the timeout period of 50 seconds. Check the log output for error information.
      Microsoft.AspNetCore.SpaServices.Extensions.Util.TaskTimeoutExtensions.WithTimeout(Task task, TimeSpan timeoutDelay, string message)

      Stack Query Cookies Headers
      TimeoutException: The Angular CLI process did not start listening for requests within the timeout period of 50 seconds. Check the log output for error information.
      Microsoft.AspNetCore.SpaServices.Extensions.Util.TaskTimeoutExtensions.WithTimeout(Task task, TimeSpan timeoutDelay, string message)
      Microsoft.AspNetCore.SpaServices.Extensions.Proxy.SpaProxy.PerformProxyRequest(HttpContext context, HttpClient httpClient, Task baseUriTask, CancellationToken applicationStoppingToken, bool proxy404s)
      Microsoft.AspNetCore.Builder.SpaProxyingExtensions+c__DisplayClass2_0+<b__0>d.MoveNext()
      Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
      Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
      Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

      Like

  6. Hi Neel,
    Thank you for your response. Application is working fine now locally.
    Publishing this app over Azure (app service) from Visual Studio 2017 getting below error.

    InvalidOperationException: The SPA default page middleware could not return the default page ‘/index.html’ because it was not found, and no other middleware handled the request.
    Your application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.

    Like

  7. It would be really nice if you post to how to compile and to publish at once.

    I meant debugging is easy, but to publish it I need to compile Angular 6 in prod and and publish core 2.1 seperately and move the files around

    Thanks !! in advance

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s