Extension Methods

UmbNav provides extension methods on `UmbNavItem` for programmatic rendering and URL generation. These are useful when you need more control than the TagHelper provides.

Available Methods

Url()

Gets the resolved URL for a menu item.

string Url(string? culture = null, UrlMode mode = UrlMode.Default)

Parameters:

Parameter
Type
Default
Description

culture

string?

null

Culture code for multi-language sites

mode

UrlMode

Default

URL generation mode

Returns: The URL string, or "#" if no URL is available.

Example:

// Basic usage
var url = item.Url();
// Output: "/about-us/"

// With culture
var url = item.Url("da-DK");
// Output: "/da/om-os/"

// Absolute URL
var url = item.Url(mode: UrlMode.Absolute);
// Output: "https://example.com/about-us/"

IsActive()

Checks if a menu item is active (matches or is an ancestor of the current page).

Parameters:

Parameter
Type
Default
Description

currentPage

IPublishedContent

Required

The current page to check against

minLevel

int?

null

Minimum level to consider (skip top levels)

includeDescendants

bool

false

Also check if current page is a descendant in the content tree

Returns: true if the item is active or an ancestor of the active item.

Example:

GetLinkHtml()

Generates the complete HTML for a menu item link.

Parameters:

Parameter
Type
Default
Description

cssClass

string?

null

CSS class(es) to add

id

string?

null

HTML id attribute

culture

string?

null

Culture for URL generation

mode

UrlMode

Default

URL generation mode

labelTagName

string

"span"

Tag for text items

htmlAttributes

object?

null

Anonymous object of additional attributes

activeClass

string?

null

Class to add when item.IsActive is true

Returns: IHtmlContent that can be rendered directly.

Example:

GetItemHtml()

Alias for GetLinkHtml() - provided for semantic clarity.

Usage Examples

Basic Navigation

With Active State

Bootstrap Dropdown

Building URLs Programmatically

Conditional Rendering

With Images

Multi-Language Support

Combining with Menu Builder Service

For complex scenarios, combine extension methods with the Menu Builder Service:

Extension Methods vs TagHelper

Feature
Extension Methods
TagHelper

Syntax

@item.GetLinkHtml()

<umbnavitem>

Flexibility

High

Medium

Code reuse

Moderate

High

Readability

Good for simple cases

Better for complex markup

Custom attributes

Via anonymous object

Not directly supported

Performance

Similar

Similar

Use Extension Methods when:

  • You need custom HTML attributes

  • You're building complex, conditional markup

  • You need the URL without the full element

  • You're working in C# code (controllers, services)

Use TagHelper when:

  • You want clean, declarative Razor syntax

  • You're building straightforward navigation

  • You want consistency across views

Namespace

Remember to include the namespace:

Or add to _ViewImports.cshtml:

Last updated

Was this helpful?