c# - 将 ASP.NET Core 2.1 和 Angular 6 项目发布到 Azure

标签 c# asp.net angular azure asp.net-web-api

我一直在尝试将项目发布到 Azure Web 服务。我使用 dotnet cli 工具生成了项目,当它生成项目时,我删除了提供的 ClientApp 文件夹并使用 Angular CLI 生成了一个新的 Angular 项目。现在我在前端使用 Angular 6,在后端使用 Entity Framework Core 的 Asp.NET core 2.1。我编写了一个简单的网站,可以在我的本地主机上完美运行。现在我想将其发布到Azure。发布过程本身是成功的,我可以看到下面的一条消息,但是当 URL 在浏览器中打开时,它会显示以下内容: enter image description here 然后我尝试在 Azure 上诊断并解决问题,这就是我得到的结果: enter image description here 完整报告: enter image description here

在发布过程中我配置了一些设置:

enter image description here

但还是没有运气。之前我对 core 2.0 和 Angular 5 使用了相同的方法,效果很好。现在我遇到了 core 2.1 和 Angular 6 的问题

这是 Startup.cs:

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using MovieApp.Models;
using System.IO;
using System.Text;

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

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("EnableCORS", builder =>
            {
                builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials().Build();
            });
        });

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,
                ValidIssuer = "http://localhost:63269",
                ValidAudience = "http://localhost:63269",
                // ValidIssuer = "https://nqmoviesng.azurewebsites.net",
                //  ValidAudience = "https://nqmoviesng.azurewebsites.net",
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345"))
            };
        });
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddMvc();


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

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseAuthentication();
        app.UseCors("EnableCORS");

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseDefaultFiles();
        app.UseStaticFiles();

        app.UseMvcWithDefaultRoute();

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

.csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\** 
</DefaultItemExcludes>

<!-- Set this to true if you enable server-side prerendering -->
<BuildServerSideRenderer>false</BuildServerSideRenderer>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

有什么想法吗?

最佳答案

我更改了 angular.json 文件。将“outputPath”:“dist/ClientApp”替换为“outputPath”:“dist”并且它起作用了!

关于c# - 将 ASP.NET Core 2.1 和 Angular 6 项目发布到 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51798614/

相关文章:

c# - NSec.Cryptography 使用 ChaCha20Poly1305 和 SharedSecret 进行加密和解密

c# - 具有运行时映射配置的自动映射器

c# - 从 Angular 2 到 ASP.NET ApiController 的 Http.post

c# - 枚举 Linq.Xelement

c# - 如何从 blazor 中的 EventCallback 获取返回值?

c# - 扫描自定义属性的所有类和方法的最佳实践

javascript - 将图像复制到剪贴板在 Firefox 和 Chrome 中不起作用

angular - 在 Angular 7 中将数据恢复为 react 形式

javascript - 嵌套的 json 需要借助 Angular 以树结构格式动态显示

javascript - HttpParams 设置为 null 进行调用