PiranhaCMS / piranha.core

Piranha CMS is the friendly editor-focused CMS for .NET that can be used both as an integrated CMS or as a headless API.
http://piranhacms.org
MIT License
1.97k stars 553 forks source link

Integrating into existing .NET Core 3.1/5 #1366

Closed zoinkydoink closed 3 years ago

zoinkydoink commented 3 years ago

I seem to be overwhelmed on what is actually required to integrate the CMS into an existing site.

I simply want to use the CMS as a backend.

  1. Add pages to cms using ~/manager
  2. Consume those page contents on my own pages
  3. Is it possible to have /mycustom page display content of page1 and /page1 to display it as well or do i get to pick one or the other?
  4. I prefer to simply use it as the backend but not sure what controls I need to copy from a default piranha website/startup.cs

I think it would be extremely beneficial for everyone to take a bare bone ASP.NET Core website and add Piranha to it so the routes are not altered but still able to utilize the functionality.

Can you let me know or even better (put a tutorial together that goes in the docs) that covers the above? If you do not have time, you can just piece mill it and I can try to put it together.

Razor ASP.NET Core Startup.cs

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

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }

Piranha Startup.cs

  public class Startup
    {
        private readonly IConfiguration _config;

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="configuration">The current configuration</param>
        public Startup(IConfiguration configuration)
        {
            _config = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // Service setup
            services.AddPiranha(options =>
            {
                options.AddRazorRuntimeCompilation = true;

                options.UseFileStorage(naming: Piranha.Local.FileStorageNaming.UniqueFolderNames);
                options.UseImageSharp();
                options.UseManager();
                options.UseTinyMCE();
                options.UseMemoryCache();
                options.UseEF<SQLiteDb>(db =>
                    db.UseSqlite(_config.GetConnectionString("piranha")));
                options.UseIdentityWithSeed<IdentitySQLiteDb>(db =>
                    db.UseSqlite(_config.GetConnectionString("piranha")));

                /***
                 * Here you can configure the different permissions
                 * that you want to use for securing content in the
                 * application.
                options.UseSecurity(o =>
                {
                    o.UsePermission("WebUser", "Web User");
                });
                 */
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApi api)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // Initialize Piranha
            App.Init(api);

            // Build content types
            new ContentTypeBuilder(api)
                .AddAssembly(typeof(Startup).Assembly)
                .Build()
                .DeleteOrphans();

            // Configure Tiny MCE
            EditorConfig.FromFile("editorconfig.json");

            // Middleware setup
            app.UsePiranha(options => {
                options.UseManager();
                options.UseTinyMCE();
                options.UseIdentity();
            });
        }
    }
tidyui commented 3 years ago

Hi there! In ConfigureService you can specify which parts of the application you want to disable. For example:

public void ConfigureServices(IServiceCollection services)
{
    // Service setup
    services.AddPiranha(options =>
    {
        options.UseArchiveRouting = false;
        options.UsePageRouting = false;
        options.UsePostRouting = false;
        options.UseStartpageRouting = false;

        ...
    }
}

would disable the routing features for:

This means that no request rewriting will take place and your existing application can just get the data it wants from the IApi service.

Best regards

tidyui commented 3 years ago

You can find the full set of configuration options here:

https://piranhacms.org/docs/basics/getting-started

zoinkydoink commented 3 years ago

I have changed startup.cs to following but / results in 404, if i do /index than it pulls my index page (custom), something is still messing with the regular asp.net routing.

I have also attached the zip of the project if that helps WebApplication2.zip

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Piranha;
using Piranha.AttributeBuilder;
using Piranha.AspNetCore.Identity.SQLite;
using Piranha.Data.EF.SQLite;
using Piranha.Manager.Editor;

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

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddRazorPages()
                .AddPiranhaManagerOptions();

            services.AddPiranha(options =>
            {
                options.UseArchiveRouting = false;
                options.UsePageRouting = false;
                options.UsePostRouting = false;
                options.UseStartpageRouting = false;

            });
            services.AddPiranhaApplication();
            services.AddPiranhaFileStorage();
            services.AddPiranhaImageSharp();
            services.AddPiranhaManager();
            services.AddPiranhaTinyMCE();
            services.AddPiranhaMemoryCache();

            services.AddPiranhaEF<SQLiteDb>(db =>
                db.UseSqlite("Filename=./piranha.razorweb.db"));
            services.AddPiranhaIdentityWithSeed<IdentitySQLiteDb>(db =>
                db.UseSqlite("Filename=./piranha.razorweb.db"));
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApi api)
        {
            App.Init(api);

            App.CacheLevel = Piranha.Cache.CacheLevel.Basic;

            new ContentTypeBuilder(api)
                .AddAssembly(typeof(Startup).Assembly)
                .Build()
                .DeleteOrphans();

            EditorConfig.FromFile("editorconfig.json");

            app.UseStaticFiles();
            app.UsePiranha();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UsePiranhaIdentity();
            app.UsePiranhaManager();
            app.UsePiranhaTinyMCE();
            app.UseEndpoints(endpoints =>
            {

                endpoints.MapRazorPages();
                endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

                endpoints.MapPiranhaManager();
            });
        }
    }
}
tidyui commented 3 years ago

I understand your confusion, the documentation has been updated a lot of times as the startup functionality has changed. As of 9.0 we're consolidating a lot of methods and we will rewrite the docs to make it easier to understand the setup options.

I have pushed an example repo where Piranha is only used as a passive backend for a regular Razor Pages application. You can find it here:

https://github.com/tidyui/piranha-backend

Best regards

Håkan

tidyui commented 3 years ago

@zoinkydoink Did you get the example repository to work?

zoinkydoink commented 3 years ago

@tidyui i did get that example running, but converting it to .net 5 and integrating rest of my projects seem to cause an error, i opened another issue here https://github.com/PiranhaCMS/piranha.core/issues/1370#issuecomment-710762927

zoinkydoink commented 3 years ago

I see that you closed the other issue, but please know that just simply adding a reference to SQL server library crashes the app (sql server is used for other things within my app and not for piranha)

tidyui commented 3 years ago

@zoinkydoink You can see the comment on how SqlServer here #1370. Regarding .NET 5 we have never tried it, so there's no guarantee that Piranha is compatible with it. We usually don't look at migrating until versions are officially released.