Sal
Sal

Reputation: 53

Asp.net core 2 with angular 6 template

I am looking for a template to use asp.net core 2.0 with angular 6 in one solution with f5 hit to run the application.

Can you help find such request ?

Thanks

Upvotes: 5

Views: 4637

Answers (2)

Juan G Carmona
Juan G Carmona

Reputation: 2208

I created one, it is on GitHub, it could be a good starting point but it is not perfect, it should grow and evolve during next months...

https://github.com/JuanGarciaCarmona/AspNetCore21Ang6Template

Also there is an article in Code Project that explains how to use it.

https://www.codeproject.com/Articles/1246748/Angular-within-ASP-NET-Core

I hope it helps.

Upvotes: 0

Lubos Zapotocny
Lubos Zapotocny

Reputation: 105

tutorial here: I would not use

Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0-rc1-final

but

Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0

You can use Angular6.

The magic is that .net core starts new command line itself and runs npm script.

app.UseSpa(spa =>
{
    // To learn more about options for serving an Angular SPA from ASP.NET Core,
    // see https://go.microsoft.com/fwlink/?linkid=864501

    spa.Options.SourcePath = "ClientApp";

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

npm start is by default alias for ng serve.

I do have working project with Angular6 and Core 2.

My project.csproj

...
<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
  <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
  <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
  <IsPackable>false</IsPackable>

  <SpaRoot>ClientApp\</SpaRoot>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
  <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.0.0" />
</ItemGroup>
...

and Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.SpaServices.AngularCli;


namespace AngularSPA
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            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");
                }
            });
        }
    }
}

enter image description here

But I only have aesthetic problem with built-in command line, you can see my question here.

Upvotes: 2

Related Questions