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.92k stars 549 forks source link

piranha manager json serialization #2070

Open VikneshSubramaniyan opened 1 month ago

VikneshSubramaniyan commented 1 month ago

The Piranha manager is breaking when we perform JSON serialization in AddMVC, so I called the serialization inside the Piranha manager. However, JSON serialization is still not working. Please help us.

public class Startup { public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; var contentRoot = configuration.GetValue(WebHostDefaults.ContentRootKey); }

public IConfiguration Configuration { get; }
public string conn; 
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // Get connection strings from the appsettings.json file
    var defaultConnection = Configuration.GetConnectionString("DefaultConnection");
    var advConnection = Configuration.GetConnectionString("ADVConnection");
    conn = advConnection;

    // Set up all the database services
    /*
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(defaultConnection));
    services.AddDbContext<Db>(options => options.UseSqlServer(defaultConnection));
    services.AddDbContext<AdvDbContext>(options => options.UseSqlServer(advConnection));
    */
    // BB 5/16/2018 - make compatible with SQL Server 2008R2
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(defaultConnection, b => b.UseRowNumberForPaging()));
    services.AddDbContext<SQLServerDb>(options => options.UseSqlServer(defaultConnection, b => b.UseRowNumberForPaging()));

         services.AddScoped<IApi, Api>();

    // Allow the this application to use the Piranha manager
    services.AddPiranha(options =>
    {
        options.UseCms();
        options.UseManager(o => {
            o.JsonOptions = (jsonOptions) =>
            {
                jsonOptions.SerializerSettings.ContractResolver = new DefaultContractResolver();
                jsonOptions.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;

            };
        });
        options.UseFileStorage(naming: Piranha.Local.FileStorageNaming.UniqueFolderNames);
        options.UseImageSharp();

        options.UseTinyMCE();

                    options.UseEF<SQLServerDb>(db =>
            db.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
               });

   //services.AddMvc(options => options.EnableEndpointRouting = true)
    //   .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
    return services.BuildServiceProvider();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services)
{

    // Show useful error messages if the application is in development
    if (env.IsDevelopment())
    {
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    DefaultTypeMap.MatchNamesWithUnderscores = true;

    // Initialize Piranha
    var api = services.GetService<IApi>();
    App.Init(api);

          var pageTypeBuilder = new ContentTypeBuilder(api)
        .AddType(typeof(BlogArchive))
        .AddType(typeof(StandardPage));
    pageTypeBuilder.Build(); 
    var postTypeBuilder = new ContentTypeBuilder(api)
        .AddType(typeof(BlogPost));
   postTypeBuilder.Build(); 
    EditorConfig.FromFile("editorconfig.json");

    // Register middleware
    app.UseStaticFiles();
    //app.UseRouting();
    app.UseAuthentication();
  // app.UseAuthorization();
    app.UseMiddleware<PasswordMiddleware>();

          app.UsePiranha(options =>
    {
        options.UseManager();
        options.UseTinyMCE();
    });
    //app.UsePiranhaIdentity();

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

}

tidyui commented 1 month ago

I think the problem here is that the Piranha manager just uses the default MVC serialization that is applied using the [ApiController] attribute. If you fundamentally change how JSON is serialized EVERYTHING will break since the JSON data that the Vue application gets will be different.

The solution to this is of course that the entire Piranha Manager should use it's own isolated JSON settings that can be vastly different from the settings of the main application, but unfortunately that's not how it works currently.

tidyui commented 3 weeks ago

If I'm not mistake .NET supports configuring different JsonSettings per Area, so a workaround for this would be to configure custom application settings for a specific area where certain endpoints run.