Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

piranha manager json serialization #2070

Open
VikneshSubramaniyan opened this issue May 17, 2024 · 2 comments
Open

piranha manager json serialization #2070

VikneshSubramaniyan opened this issue May 17, 2024 · 2 comments
Labels

Comments

@VikneshSubramaniyan
Copy link

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
Copy link
Member

tidyui commented May 17, 2024

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
Copy link
Member

tidyui commented Jun 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants