https://discord.umbraco.com logo
Our Umbraco project was using PascalCase
# umbraco-chat
j
Our Umbraco project was using PascalCase in v13. We set the Naming Policy to pascal case. After upgrading to v17, the back office is not functioning correctly (deployment and environment information are missing), I found that the Umbraco back office frontend expect camelCase from the response. Is it possible to configure our controller to use PascalCase while keeping Umbraco back office using camelCase globally?
m
Probably one for the forum.umbraco.com... but simplest might be
return new JsonResult(obj, new JsonSerializerOptions());
Default is Pascal case.. if you wanted other casing.. it would be
return new JsonResult(obj,new JsonSerializerOptions{PropertyNamingPolicy = JsonNamingPolicy.KebabCaseLower });
ps
using System.Text.Json;
j
😂 I know this approach works, but I’d rather not have to apply it in every method. I ended up creating a PascalCase attribute and apply to our controllers instead. I’m wondering why it worked in v13 but not in v14, could it be because Umbraco removed Newtonsoft?
m
I think I have a memory that json was passed through an angular process previously, which is no more, maybe that had an effect.. As a though I asked AI re checking namespace for setting.. Came back with.... Replace "YourApp.Controllers" with your namespace. This only affects matching controllers' JSON outputs. This keeps global defaults intact while automating for your namespace—no forgetting on new controllers.
Copy code
csharp
// PascalCaseJsonOutputFormatter.cs
public class PascalCaseJsonOutputFormatter : JsonOutputFormatter
{
    private readonly string _targetNamespace;

    public PascalCaseJsonOutputFormatter(ILogger logger, JsonSerializerOptions options, string targetNamespace)
        : base(options, logger)
    {
        _targetNamespace = targetNamespace;
    }

    public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
    {
        var controller = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ControllerActionDescriptor>()?.ControllerTypeInfo;
        if (controller?.Namespace?.StartsWith(_targetNamespace) != true)
        {
            // Fallback to default formatter logic or throw/skip
            await base.WriteResponseBodyAsync(context, selectedEncoding);
            return;
        }

        // Use PascalCase (null policy)
        Options.JsonSerializerOptions.PropertyNamingPolicy = null;
        await base.WriteResponseBodyAsync(context, selectedEncoding);
    }
}
IComposer or program.cs
Copy code
csharp
builder.Services.AddControllers(options =>
{
    var pascalFormatter = new PascalCaseJsonOutputFormatter(
        loggerFactory.CreateLogger<PascalCaseJsonOutputFormatter>(),
        new JsonSerializerOptions(),
        "YourApp.Controllers"  // Your namespace
    );
    options.OutputFormatters.Insert(0, pascalFormatter);
});
j
That makes sense. I’ll give a try. Thank you!