gpt4 book ai didi

Asp.net core 2 与 Angular 6 模板

转载 作者:行者123 更新时间:2023-12-02 15:56:24 25 4
gpt4 key购买 nike

我正在寻找一个模板,以便在一个解决方案中使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com