【问题标题】:Piranha CMS with AngularPiranha CMS 与 Angular
【发布时间】:2021-02-24 23:37:28
【问题描述】:

我是 .NET Core 和 Angular 的新手。我想通过 .NET Core 和 Angular 构建一个博客站点 我找到了这个github: https://libraries.io/github/PiranhaCMS/piranha.core.angular

我知道如何安装,但不知道如何使整个项目运行。 我已经尝试使用与该项目相同的版本,我的想法是先尝试使整个项目启动并运行,然后逐步升级版本。 我尝试先运行后端,但是当我浏览 https://localhost:5001 时它会显示

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request.
Your application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.

当我尝试通过命令ng serve 运行角度前端时, 它显示了

An unhandled exception occurred: No projects support the 'build' target.
See "/private/var/folders/qz/nshtnxp52h1cv72sj6vzp46w0000gn/T/ng-7qC8Wl/angular-errors.log" for further details.

有人知道如何让整个项目启动并运行吗?

【问题讨论】:

    标签: asp.net angular piranha-cms


    【解决方案1】:

    以下是我设置 Piranha CMS + Angular 的方法。我的要求是将 Piranha 作为无头 CMS 运行,Angular 负责前端。

    撰写本文时我的环境:

    • .NET 5.0
    • 食人鱼 9.0.1
    • Piranha.AspNetCore.Identity 9.0.0
    • Piranha.AspNetCore.Identity.PostgreSQl 9.0.0
    • Piranha.Data.EF.PostgreSql 9.0.0
    • Piranha.Manager 9.0.1
    1. 像 Håkan Edling 提到的那样设置空食人鱼项目。
    dotnet new -i Piranha.Templates
    dotnet new piranha.empty
    
    1. 为 Piranha CMS 和应用程序设置身份。棘手的情况是,如果您安装 Piranha.AspNetCore.Identity,该包会覆盖身份配置,从而难以自定义您自己的用户和角色。我的方法是将 Piranha Identity 和 Application Identity 分开。

    首先,设置食人鱼身份

     options.UseIdentityWithSeed<IdentityPostgreSQLDb>(
                        db => db.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")),
                        ConfigureIdentity,
                        cookieOptions: co => // This part is optional
                        {
                            co.Cookie.Name = "YourOwnCookieName";
                            co.LoginPath = "/manager/login";
                            co.AccessDeniedPath = "/manager/login";
                            ...
                        }
                    );
    

    接下来,设置应用程序身份。在这里,我使用 ApplicationUser 作为您应用程序的自定义用户。

    var identityBuilder = services.AddIdentityCore<ApplicationUser>(ConfigureIdentity);
    identityBuilder = new IdentityBuilder(identityBuilder.UserType, typeof(IdentityRole), identityBuilder.Services);
    identityBuilder.AddEntityFrameworkStores<ApplicationDbContext>();
    identityBuilder.AddRoleValidator<RoleValidator<IdentityRole>>();
    identityBuilder.AddRoleManager<RoleManager<IdentityRole>>();
    identityBuilder.AddSignInManager<SignInManager<ApplicationUser>>();
    identityBuilder.AddDefaultTokenProviders();
    

    这是 ConfigureIdentity 的代码,您可以根据需要对其进行自定义。

    private void ConfigureIdentity(IdentityOptions options)
            {
                options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultPhoneProvider;
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 5;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireLowercase = false;
                options.Password.RequireUppercase = false;
                options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
                options.User.RequireUniqueEmail = false;
            }
    

    由于我使用 PostgreSql 作为数据库,Piranha Identity 和您的 Identity 之间存在涉及索引名称的冲突,要解决这些问题,您可以在 ApplicationDbContext OnModelCreating 中使用此代码重命名索引

    builder.Entity<IdentityRole>().HasIndex(e => e.NormalizedName).HasDatabaseName("role_name_index");
    builder.Entity<ApplicationUser>().HasIndex(e => e.NormalizedEmail).HasDatabaseName("email_index");
    builder.Entity<ApplicationUser>().HasIndex(e => e.NormalizedUserName).HasDatabaseName("user_name_index");
    

    此时,您有两个 Identity 正在运行。如果您设置迁移,Piranha 和您的应用程序都使用相同的 EFMigrationsHistory 表。如果您对此不满意并想分离迁移表,可以在设置 ApplicationDbContext 服务时添加此代码:

    services.AddDbContext<ApplicationDbContext>(options =>
                    options
                        .UseNpgsql(
                            Configuration.GetConnectionString("DefaultConnection"),
                            builder =>
                            {
                                builder.MigrationsHistoryTable("__ef_migrations_history"); // separate migrations table
                            }
                        )
                        .UseSnakeCaseNamingConvention() // this is optional, from EFCore.NamingConventions package
                );
    

    Piranha 不适用于 EFCore.NamingConventions 包,例如,如果您想使用 snake_case 命名。这里有很多选择:

    1. 将 Piranha CMS 数据库与您自己的数据库分开(您可以滚动自己的命名约定,保留 Piranha 命名约定)。
    2. 使用与 Piranha CMS 相同的命名约定。
    3. 将 Piranha CMS 数据库命名约定与您的应用程序命名约定分开(丑陋,但这是我使用的那个)。

    如果您使用选项 3 运行,在我的情况下,我希望为我的应用程序表命名为 snake_case,您必须在 ApplicationDbContext OnModelCreating 中重命名您的应用程序身份表,因为 EFCore.NamingConventions 不会重命名您的身份表。

    builder.Entity<ApplicationUser>().ToTable("asp_net_users");
    builder.Entity<IdentityRole>().ToTable("asp_net_roles");
    builder.Entity<IdentityUserToken<string>>().ToTable("asp_net_user_tokens");
    builder.Entity<IdentityUserLogin<string>>().ToTable("asp_net_user_logins");
    builder.Entity<IdentityUserClaim<string>>().ToTable("asp_net_user_claims");            
    builder.Entity<IdentityUserRole<string>>().ToTable("asp_net_user_roles");
    builder.Entity<IdentityRoleClaim<string>>().ToTable("asp_net_role_claims");
    
    1. 此时,Piranha CMS 将与您的应用程序分开运行。您可以安装 Piranha.WebApi 包以从 API 访问食人鱼内容,也可以滚动您自己的 API。 Piranha.WebApi 的代码很简单,您可以轻松地从他们的存储库中自定义代码:https://github.com/PiranhaCMS/piranha.core/tree/master/core/Piranha.WebApi。如果您不关心您的内容授权,您可以添加此选项以启用匿名访问:
    services.AddPiranha(options =>
                {
                    options.UseApi(o => o.AllowAnonymousAccess = true);                
                });
    

    最后,你可以禁用 Piranha 路由

    services.AddPiranha(options =>
                {
                    options.DisableRouting();
                });
    

    【讨论】:

      【解决方案2】:

      作为 Piranha CMS 的官方维护者,您所指的项目是一个社区项目,似乎已被其维护者放弃并且不再可用。

      我建议您按照the docs 设置一个空的食人鱼应用程序,创建一个自定义 ApiController 并创建调用该 API 的 Angular Web 应用程序。

      【讨论】:

        猜你喜欢
        • 2019-06-28
        • 1970-01-01
        • 2013-11-11
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 2022-12-14
        • 2013-11-26
        相关资源
        最近更新 更多