Umbraco

Once you have successfully installed Umbraco 9 you probably only have one .NET project in your solution. However, if you are like me, then you might want to separate concerns and move any generated ModelsBuilder classes into its own separate .NET class library project.

The good news is that this can be done fairly simply by updating the appsettings.json file in your project. Below is an example of a working ModelsBuilder configuration:

{
  "$schema": "./umbraco/config/appsettings-schema.json",
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "System": "Warning"
      }
    }
  },
  "ConnectionStrings": {
    "umbracoDbDSN": "server=***;database=***;user id=***;password='***'"
  },
  "Umbraco": {
    "CMS": {
      "Hosting": {
        "Debug": false
      },
      "ModelsBuilder": {
        "ModelsMode": "SourceCodeManual",
        "ModelsDirectory": "~/../DanMcilroy.Umbraco.Models",
        "AcceptUnsafeModelsDirectory": true
      },
      "Content": {
        "ContentVersionCleanupPolicy": {
          "EnableCleanup": true
        }
      },
      "Global": {
        "Id": "84f92735-7303-47b1-ab26-0c69b1e8999a"
      }
    }
  }
}

The important part is the ModelsBuilder section under Umbraco. Here you need to specify three properties; ModelsMode, ModelsDirectory and AcceptUnsafeModelsDirectory. For more details on the different properties and values please visit the official Umbraco 9 ModelsBuilder Configuration page. The three properties we are interested in are the following:

  • Umbraco.CMS.ModelsBuilder.ModelsMode determines how Models Builder generates models. Valid values are:

    • Nothing: Do not generate models.
    • InMemoryAuto(default): Generate models in a dynamic in-memory assembly.
    • SourceCodeManual: Generate models in ~/umbraco/models (but do not compile them) whenever the user clicks the "Generate models" button on the Models Builder dashboard in the Settings section.
    • SourceCodeAuto: Generate models in ~/umbraco/models (but do not compile them) anytime a content type changes.
  • Umbraco.CMS.ModelsBuilder.ModelsDirectory (string, default is ~/umbraco/models) indicates where to generate models and manage all files. Has to be a virtual directory (starting with ~/) below the website root (see also: AcceptUnsafeModelsDirectory below).

  • Umbraco.CMS.ModelsBuilder.AcceptUnsafeModelsDirectory (bool, default is false) indicates that the directory indicated in ModelsDirectory is allowed to be outside the website root (e.g. ~/../../some/place). Due to this being a potential security risk, it is not allowed by default.

Once the ModelsDirectory has been set to your new project and the AcceptUnsafeModelsDirectory set to true, you can reload the Umbraco backoffice and under the ModelsBuilder tab click the Generate Models button.

Models Builder Generate models

You will then see the generated ModelsBuilder classes populated in your new separate .NET project.

ModelsBuilder Solution