LESSON 16 OF 20

16. AdSense-Compliant Content Layout and User Experience

In this lesson, we will build a mobile-friendly article template that keeps original content central, separates advertisements clearly, and supports a reliable user experience.

45 dk 32

Introduction

In a content management application, retrieving an article from the database is only part of the job. The way the article is presented affects readability, mobile usability, accessibility, SEO, and the way users interact with advertising. In this lesson, we will use the SEO-friendly content structure from the previous lesson to create a safer and more maintainable article template with clearly separated advertising areas.

The goal is not to guarantee approval from a particular advertising provider. Instead, the goal is to make original and useful content the primary focus, keep advertisements visually and semantically distinct from editorial content, avoid presenting ads as navigation or download controls, and provide a consistent experience across screen sizes. Advertising policies can change, so always review the current provider requirements before deploying to production.

Core principle: When users arrive at a page, they should be able to find the information they came for without advertisements blocking the content or encouraging accidental clicks.

Core concepts of an AdSense-compliant layout

Separating content from advertisements

An advertising area should not be confused with the article title, category links, pagination controls, or download links. A clear label such as “Advertisement” helps communicate the purpose of the area. Do not use arrow graphics, fake buttons, or navigation-like styling to draw attention to an advertisement.

The amount of advertising should also be reasonable compared with the amount of content. Screens that contain little or no content, exist only to display ads, or force users to encounter an ad before completing a normal action create a poor experience. It is usually better not to render advertising areas on error pages, thank-you pages, empty search results, or other screens with little editorial value.

Thinking mobile-first

Horizontal overflow, very small tap targets, fixed advertisements covering content, and advertising placed too close to navigation controls are common mobile problems. Design the narrow-screen flow first, then expand the layout into columns for larger screens.

The article title, author details, publication date, content, and related links should remain readable and logically ordered on mobile devices. An advertising area should not be fixed over the content with a hard-coded overlay. It should occupy space in the normal document flow. This also helps reduce disruptive movement when an ad loads later.

Useful and trustworthy content

Advertising placement cannot compensate for poor editorial quality. An article should answer a real question, match its title, and provide useful information. It should not be filled with automatically generated or unreviewed text. Avoid statements such as “click an ad to support us,” because they explicitly encourage users to interact with advertisements.

Creating the page model

First, let us collect the data required by the article page in one ViewModel. This prevents the view from depending directly on a database entity and allows presentation-related options, such as advertising visibility, to be passed in a controlled way.

public sealed class ArticlePageViewModel
{
    public int Id { get; init; }
    public string Title { get; init; } = string.Empty;
    public string Slug { get; init; } = string.Empty;
    public string Summary { get; init; } = string.Empty;
    public string ContentHtml { get; init; } = string.Empty;
    public string AuthorName { get; init; } = string.Empty;
    public DateTime PublishedAtUtc { get; init; }
    public string CategoryName { get; init; } = string.Empty;
    public IReadOnlyList RelatedArticles { get; init; }
        = Array.Empty<RelatedArticleViewModel>();
    public bool ShowTopAd { get; init; }
    public bool ShowInlineAd { get; init; }
}

public sealed class RelatedArticleViewModel
{
    public string Title { get; init; } = string.Empty;
    public string Slug { get; init; } = string.Empty;
}

Properties such as ShowTopAd and ShowInlineAd do not place advertising code inside the model. They only control whether an area is rendered. The actual provider integration should be managed separately through a component, partial view, or deployment-specific configuration.

Building the model in the controller

The controller should obtain the article and related content through the service layer. This preserves the architecture established in earlier lessons and prevents the view from performing database queries.

public sealed class ArticlesController : Controller
{
    private readonly IArticleService _articleService;

    public ArticlesController(IArticleService articleService)
    {
        _articleService = articleService;
    }

    [HttpGet("article/{slug")]
    public async Task<IActionResult> Details(string slug, CancellationToken cancellationToken)
    {
        var article = await _articleService.GetPublishedBySlugAsync(
            slug, cancellationToken);

        if (article is null)
        {
            return NotFound();
        }

        var model = new ArticlePageViewModel
        {
            Id = article.Id,
            Title = article.Title,
            Slug = article.Slug,
            Summary = article.Summary,
            ContentHtml = article.ContentHtml,
            AuthorName = article.AuthorName,
            PublishedAtUtc = article.PublishedAtUtc,
            CategoryName = article.CategoryName,
            RelatedArticles = article.RelatedArticles
                .Select(x => new RelatedArticleViewModel
                {
                    Title = x.Title,
                    Slug = x.Slug
                })
                .ToList(),
            ShowTopAd = article.ContentHtml.Length >= 1200,
            ShowInlineAd = article.ContentHtml.Length >= 2500
        };

        return View(model);
    }
}

The length check is only an example rule. Character count alone is not enough to decide whether advertising should be displayed. The usefulness of the content, the page's non-advertising value, and the effect on the reading flow must be considered together. Also make sure that the HTML has passed through a trusted sanitization process before calculating or rendering it.

Extracting the advertising area into a partial view

Moving repeated markup into a partial view keeps the article view easier to maintain. Microsoft’s ASP.NET Core documentation describes partial views as a way to break large views into smaller pieces and reduce repeated markup. If complex data preparation or execution logic is needed, a View Component may be more appropriate.

Create Views/Shared/_AdPlaceholder.cshtml:

@model string

<aside class="ad-slot" aria-label="Advertisement">
    <p><strong>Advertisement</strong></p>
    <div data-ad-placement="@Model">
        <p>Advertising area</p>
    </div>
</aside>

This example intentionally does not include real provider code. The placeholder helps us test the position and dimensions of the area during development. In production, follow the provider’s current integration requirements. The aria-label explains the purpose of the region to assistive technologies, but the most important accessibility and policy considerations are the visual and semantic separation of the advertisement from the surrounding content.

Creating the article view

In Views/Articles/Details.cshtml, declare the model and divide the page into meaningful sections.

@model ArticlePageViewModel
@{
    ViewData["Title"] = Model.Title;
    ViewData["Description"] = Model.Summary;
}

<main class="article-page">
    <article>
        <header class="article-header">
            <p>@Model.CategoryName</p>
            <h2>@Model.Title</h2>
            <p>Author: @Model.AuthorName</p>
            <time datetime="@Model.PublishedAtUtc.ToString("O")">
                @Model.PublishedAtUtc.ToLocalTime().ToString("yyyy-MM-dd")
            </time>
        </header>

        @if (Model.ShowTopAd)
        {
            <partial name="_AdPlaceholder" model='"top"' />
        }

        <div class="article-summary">
            <p>@Model.Summary</p>
        </div>

        <div class="article-content">
            @Html.Raw(Model.ContentHtml)
        </div>

        @if (Model.ShowInlineAd)
        {
            <partial name="_AdPlaceholder" model='"inline"' />
        }
    </article>

    @if (Model.RelatedArticles.Count > 0)
    {
        <aside class="related-content">
            <h3>Related content</h3>
            <ul>
                @foreach (var related in Model.RelatedArticles)
                {
                    <li>
                        <a asp-controller="Articles"
                           asp-action="Details"
                           asp-route-slug="@related.Slug">
                            @related.Title
                        </a>
                    </li>
                }
            </ul>
        </aside>
    }
</main>

If ContentHtml comes from the database, using Html.Raw requires careful consideration. The value should only come from a trusted editorial workflow, be restricted to an allow-list of HTML elements, and be sanitized against XSS. Printing raw HTML submitted directly by users without sanitization can create a serious security vulnerability.

In this example, the advertisement does not behave like a title or an editorial link. It is rendered as a separate aside and remains part of the normal content flow. Related content is placed in a separate section. This helps users distinguish editorial navigation from advertising.

Layout and accessibility considerations

The shared layout should provide the viewport metadata needed for mobile rendering, semantic page regions, and optional title sections where appropriate. We are not rewriting the entire layout in this lesson, but the article template must integrate with it correctly. Each page should have one primary content area, a logical heading hierarchy, and links that can be reached and understood with a keyboard.

The following principles can guide the CSS implementation:

  • Limit the content column to a readable maximum width.
  • Move the sidebar below the main article on narrow screens.
  • Do not fix advertisements over the content or place them immediately beside menu controls.
  • Prevent long titles from creating horizontal overflow.
  • Give links and interactive controls sufficiently large tap areas.
  • Do not communicate important information through color alone; include text or semantic indicators.

Common mistakes

  1. Making an ad look like navigation: Placing an advertisement among navigation links, next to a download control, or inside a fake button increases the risk of accidental interaction.
  2. Overloading the page with ads: Showing many advertisements before the article title makes it difficult for users to reach the information they requested.
  3. Covering content with fixed ads: On mobile devices, an area without a usable close control or one that overlaps text damages the user experience.
  4. Rendering untrusted HTML: Html.Raw is not safe without sanitization and an explicit allow-list of permitted HTML.
  5. Testing only on desktop: Check screens around 320–375 pixels wide for horizontal overflow, wrapped titles, and accidental tap areas.
  6. Showing ads on empty or low-value screens: Error pages, thank-you pages, empty search results, and redirect-only pages may not provide enough editorial value for advertising.

Practice exercise

Improve your article detail page with the following steps:

  1. Add a ReadingTimeMinutes property to ArticlePageViewModel.
  2. Calculate an approximate reading time in the service layer by dividing the word count by 200, with a minimum value of one minute.
  3. Display the author, publication date, and estimated reading time below the article title.
  4. Use _AdPlaceholder.cshtml to keep the top and inline advertising areas separate.
  5. Verify that the advertising area is visually different from related-content links.
  6. Use browser device emulation to test narrow, landscape, and wide-screen layouts.
  7. Review how ContentHtml is produced for a user without HTML-editing permission, and document the risks of rendering raw HTML.

Conclusion

This lesson demonstrated that an advertising-supported content page is not created simply by inserting ad code. We made original and useful content the central element, collected article data in a ViewModel, and isolated advertising areas in a partial view. We reinforced the distinction between advertisements and editorial content through semantic markup, clear labeling, and careful placement.

The next step is to evaluate this template in a real deployment through performance, accessibility, and device testing. Review the advertising provider’s current policies separately, and avoid placements that encourage accidental clicks, cover content, or add advertising to low-value screens. A strong template creates a sustainable balance between monetization goals and user trust.

Sources