Asp.net core 2 与 Angular 6 模板

标签 asp.net angular6

我正在寻找一个模板,以便在一个解决方案中使用 ASP.NET Core 2.0 和 Angular 6,并通过 f5 来运行应用程序。

你能帮忙找到这样的请求吗?

谢谢

最佳答案

教程 here: 我不会使用

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

但是

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

您可以使用Angular6

神奇之处在于 .net core 启动新命令行本身并运行 npm 脚本

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 默认情况下是 ngserve 的别名。

我确实有一个使用 Angular6 和 Core 2 的工作项目。

我的项目.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>
...

和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

但我只有内置命令行的美观问题,你可以看我的question here .

关于Asp.net core 2 与 Angular 6 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50177513/

相关文章:

c# - 从自定义 .config 文件中读取

asp.net - 如何从 asp.net 中的列表中填充 gridview?

typescript - 使用 typescript 的 Angular 6 的黄金布局?

angular - 如何在 Angular 6 中构建具有多个组件的表单?

angular - 响应式(Reactive)表单验证器在 Angular 中不起作用

asp.net - 媒体资源http://url/file.mp3无法解码

ASP.NET 回发和后退按钮

javascript - 同一分页符上有 2 个脚本

android - 如何使用 WebView 从 android 应用程序调用 Angular 6 Typescript 函数?

Angular 6 ng-bootstrap 评级不起作用