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

时间:2018-08-11 10:10:25

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

我一直试图将项目发布到Azure Web服务。我使用dotnet cli工具生成了项目,当它生成项目时,我删除了提供的ClientApp文件夹,并使用Angular CLI生成了一个新的Angular项目。现在我在Entity Framework Core的前端有Angular 6,在后端有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和Angle 5使用相同的方法,但效果很好。现在我遇到了核心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>

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我更改了angular.json文件。将“ outputPath”:“ dist / ClientApp”替换为“ outputPath”:“ dist”,并且有效!