Nick Muller
Nick Muller

Reputation: 2293

Render html conditionally based on route in Razor Pages

Let's say I have the following Razor file _Layout.cshtml. How do I write the commented conditions when using ASP.NET Core 3+ and Razor Pages.

<ul class="navbar-nav">
    <li class="nav-item">
        <a class="nav-link" asp-page="/Index">Index</a>
    </li>

    @* If route starts with /a/, for example http://localhost/a/1.html and http://localhost/a/2/1.html *@
    <li class="nav-item">
        <a class="nav-link" asp-page="/a/Index">A</a>
    </li>

    @* If route starts with /b/, for example http://localhost/b/1.html and http://localhost/b/2/1.html *@
    <li class="nav-item">
        <a class="nav-link" asp-page="/b/Index">B</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" asp-page="/b/1/Index">B other</a>
    </li>
</ul>

Upvotes: 2

Views: 2410

Answers (2)

Nick Muller
Nick Muller

Reputation: 2293

You can make a Razor Pages Convention that is executed for all Razor Pages in a certain folder. With this convention you can set an Action Filter (a Result Filter) to add an identifier to the ViewData.

The filter:

class AddRouteIdentifierFilter : ResultFilterAttribute
{
    private readonly string identifier;

    public TestFilter(string identifier)
    {
        this.identifier = identifier;
    }

    public override void OnResultExecuting(ResultExecutingContext context)
    {
        base.OnResultExecuting(context);
        ((PageResult)context.Result).ViewData["route-identifier"] = identifier;
    }
}

Setting the convention:

services.AddMvc().AddRazorPagesOptions(opt => 
    opt.Conventions.AddFolderApplicationModelConvention("/A", model =>
        model.Filters.Add(new AddRouteIdentifierFilter("a"));
    );
    opt.Conventions.AddFolderApplicationModelConvention("/B", model =>
        model.Filters.Add(new AddRouteIdentifierFilter("b"));
    );
);

Access the identifier in the Razor Page view:

<h1>Identifier: @ViewData["route-identifier"]</h1>

Upvotes: 0

Peter B
Peter B

Reputation: 24247

In Razor Pages you can use ViewContext.RouteData.Values["page"], also in the _Layout file. If the URL is e.g. https://localhost:44359/Help/About then it will produce /Help/About.

Using your code:

@{
    string page = ViewContext.RouteData.Values["page"] as string; // Values[] produces objects, so cast is needed
}

<ul class="navbar-nav">
    <li class="nav-item">
        <a class="nav-link" asp-page="/Index">Index</a>
    </li>

    @if (page.StartsWith("/a/"))
    {
        <li class="nav-item">
            <a class="nav-link" asp-page="/a/Index">A</a>
        </li>
    }

    @if (page.StartsWith("/b/"))
    {
        <li class="nav-item">
            <a class="nav-link" asp-page="/b/Index">B</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" asp-page="/b/1/Index">B other</a>
        </li>
    }
</ul>

Upvotes: 1

Related Questions