nopSolutions/nopCommerce · error · Exception

No default template could be loaded

Error message

No default template could be loaded

What it means

Thrown by TopicModelFactory.PrepareTemplateViewPathAsync when neither a topic template with the given ID nor any fallback topic template exists in the system. The code tries GetTopicTemplateByIdAsync(topicTemplateId), falls back to GetAllTopicTemplatesAsync().FirstOrDefault(), and throws if both yield null — i.e. the topic templates table is empty.

Source

Thrown at src/Presentation/Nop.Web/Factories/TopicModelFactory.cs:102

        var topic = await _topicService.GetTopicBySystemNameAsync(systemName, store.Id);
        if (topic == null)
            return null;

        return await PrepareTopicModelAsync(topic);
    }

    /// <summary>
    /// Get topic template view path
    /// </summary>
    /// <param name="topicTemplateId">Topic template identifier</param>
    /// <returns>
    /// A task that represents the asynchronous operation
    /// The task result contains the view path
    /// </returns>
    public virtual async Task<string> PrepareTemplateViewPathAsync(int topicTemplateId)
    {
        var template = (await _topicTemplateService.GetTopicTemplateByIdAsync(topicTemplateId) ??
                        (await _topicTemplateService.GetAllTopicTemplatesAsync()).FirstOrDefault()) ?? throw new Exception("No default template could be loaded");

        return template.ViewPath;
    }

    #endregion
}

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Ensure the default topic templates are seeded in the database (re-run the installation/upgrade data population, or insert the standard TopicTemplate rows).
  2. In Admin > Content Management, verify topic templates exist and the topic's TemplateId references a valid row.
  3. If customizing, ensure at least one topic template row remains so the fallback works.

Example fix

// before
var template = (await _topicTemplateService.GetTopicTemplateByIdAsync(topicTemplateId) ??
                (await _topicTemplateService.GetAllTopicTemplatesAsync()).FirstOrDefault()) ?? throw new Exception("No default template could be loaded");

// after: log + fall back to a known-safe default view path
var templates = await _topicTemplateService.GetAllTopicTemplatesAsync();
var template = await _topicTemplateService.GetTopicTemplateByIdAsync(topicTemplateId)
    ?? templates.FirstOrDefault();
if (template == null)
{
    await _logger.ErrorAsync("No topic templates found; using fallback view.");
    return "Topic/TopicDetails";
}
Defensive patterns

Strategy: fallback

Validate before calling

// Ensure at least one topic template exists before rendering a topic
var templates = await _topicTemplateService.GetAllTopicTemplatesAsync();
if (!templates.Any())
    throw new InvalidOperationException("Topic templates not seeded; run data population.");

Type guard

bool HasTemplate(IList<TopicTemplate> t, int? id) => id.HasValue && t.Any(x => x.Id == id.Value) || t.Any();

Try / catch

catch (Exception exc) when (exc.Message == "No default template could be loaded")
{
    await _logger.ErrorAsync("Topic templates table is empty.", exc);
    // fall back to a safe default view path
    return "Topic/TopicDetails";
}

Prevention

When it happens

Trigger: A topic is rendered (e.g. an 'About us' or 'Shipping info' content page) and topicTemplateId does not match any row AND GetAllTopicTemplatesAsync returns an empty collection (line ~102).

Common situations: A fresh/partial database where the default topic templates were never seeded; templates were deleted by an admin or a cleanup script; the topic references a template ID that was removed during a migration; multi-store data sync dropped the template rows.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/30dfdb63e7393cf2. Report an issue: GitHub.