<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar" -->

---
title: Extending Razor with Custom Tag Helpers and View...
description: This tutorial shows how to build View Components and Custom Tag Helpers in ASP.NET Core MVC. It walks through a UserActivityViewComponent that fetches activity...
canonical: https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Extending Razor with Custom Tag Helpers and View Components in ASP.NET Core MVC | daily.dev
og:description: This tutorial shows how to build View Components and Custom Tag Helpers in ASP.NET Core MVC. It walks through a UserActivityViewComponent that fetches activity...
og:url: https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar
og:image: https://api.daily.dev/og/posts/stT1qzXAR.png
og:image:alt: Extending Razor with Custom Tag Helpers and View Components in ASP.NET Core MVC
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Extending Razor with Custom Tag Helpers and View Components in ASP.NET Core MVC

**[C\# Corner](https://daily.dev/sources/csharpcorner)** · 4 min read · 1 upvotes · 1 comments

## Summary

This tutorial shows how to build View Components and Custom Tag Helpers in ASP.NET Core MVC. It walks through a UserActivityViewComponent that fetches activity data asynchronously and renders a Default.cshtml template. It also builds a SecureSectionTagHelper that hides HTML blocks based on a user's role using IHttpContextAccessor. The guide covers registering both in _ViewImports.cshtml, invoking them from Index.cshtml, and unit testing the view component with xUnit.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.c-sharpcorner.com/article/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc>

## Questions this post answers

### How do I create a custom tag helper in ASP.NET Core that hides content based on user role?

Create a class implementing TagHelper with the HtmlTargetElement attribute targeting your custom element and a required attribute, such as [HtmlTargetElement("secure-section", Attributes = "asp-required-role")]. In ProcessAsync, set output.TagName to null to suppress the wrapper tag, check the user's role via IHttpContextAccessor and user.IsInRole, and call output.SuppressOutput() if the user lacks the role.

_Developers wiring role-based UI logic can keep ASP.NET Core patterns like this handy on daily.dev._

### How do I pass data asynchronously to a view component in ASP.NET Core MVC?

Implement InvokeAsync in a class deriving from ViewComponent, injecting a service like IUserActivityService, calling its async method, and returning View("Default", summary) with the fetched data as the model. Wrap the call in a try-catch to log errors and return a fallback Content("Unable to load activity metrics.") result if the data fetch fails.

_Those building dashboards with async widgets can track ASP.NET Core view component patterns on daily.dev._

### How do I register custom tag helpers so they work across all Razor views in an ASP.NET Core MVC project?

Add an @addTagHelper directive in Views/_ViewImports.cshtml referencing your project's assembly, such as @addTagHelper *, YourProjectAssembly, alongside the built-in @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers directive. This makes every custom tag helper in that assembly available in all views without per-page imports.

_Developers setting up shared Razor tooling can keep ASP.NET Core configuration references on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@pdfopsdev** · 0 upvotes

> IHttpContextAccessor in a tag helper for role checks works but watch out if you ever move that logic into a background service or minimal api handler, the accessor returns null outside a request scope and it's a confusing NRE to debug the first time you hit it.

## Similar posts on daily.dev

- [Understanding Partial Views in ASP.NET MVC](https://daily.dev/posts/understanding-partial-views-in-asp-net-mvc-salagpukh) · C\# Corner · 1 upvotes · 0 comments
- [htmxRazor v2.0.0: Platform and DX - Chris Woody Woodruff](https://daily.dev/posts/htmxrazor-v2-0-0-platform-and-dx---chris-woody-woodruff-gyh7wczzb) · We Are .NET · 0 upvotes · 0 comments
- [htmxRazor v1.4.0: SSE Streaming, Multi-step Wizard, and Optimistic UI - Chris Woody Woodruff](https://daily.dev/posts/htmxrazor-v1-4-0-sse-streaming-multi-step-wizard-and-optimistic-ui---chris-woody-woodruff-djatrl3bc) · We Are .NET · 20 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#c#](https://daily.dev/tags/c#), [#aspnet](https://daily.dev/tags/aspnet)

[View this post on daily.dev](https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Extending Razor with Custom Tag Helpers and View Components in ASP.NET Core MVC","url":"https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar"},"datePublished":"2026-11-18T11:36:44.960Z","dateModified":"2026-11-18T11:37:11.495Z","description":"This tutorial shows how to build View Components and Custom Tag Helpers in ASP.NET Core MVC. It walks through a UserActivityViewComponent that fetches activity...","image":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","thumbnailUrl":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","isAccessibleForFree":true,"articleSection":"C# Corner","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"C# Corner","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ada2d584df6241748fc4e71878dc70a3","url":"https://daily.dev/sources/csharpcorner"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"testing,c#,aspnet","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"C# Corner","item":"https://daily.dev/sources/csharpcorner"},{"@type":"ListItem","position":3,"name":"Extending Razor with Custom Tag Helpers and View Components in ASP.NET Core MVC"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar","comment":[{"@type":"Comment","text":"IHttpContextAccessor in a tag helper for role checks works but watch out if you ever move that logic into a background service or minimal api handler, the accessor returns null outside a request scope and it’s a confusing NRE to debug the first time you hit it.","datePublished":"2026-11-18T18:21:59.182Z","url":"https://daily.dev/posts/stT1qzXAR#c-XmmFvMyru","author":{"@type":"Person","name":"PDFops","url":"https://daily.dev/pdfopsdev","image":"https://media.daily.dev/image/upload/s---8isRBKc--/f_auto/v1782922291/avatars/avatar_orjMeK8QKaaVZwGq7ScPz?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/extending-razor-with-custom-tag-helpers-and-view-components-in-asp-net-core-mvc-stt1qzxar#faq","mainEntity":[{"@type":"Question","name":"How do I create a custom tag helper in ASP.NET Core that hides content based on user role?","acceptedAnswer":{"@type":"Answer","text":"Create a class implementing TagHelper with the HtmlTargetElement attribute targeting your custom element and a required attribute, such as [HtmlTargetElement(\"secure-section\", Attributes = \"asp-required-role\")]. In ProcessAsync, set output.TagName to null to suppress the wrapper tag, check the user's role via IHttpContextAccessor and user.IsInRole, and call output.SuppressOutput() if the user lacks the role. Developers wiring role-based UI logic can keep ASP.NET Core patterns like this handy on daily.dev."}},{"@type":"Question","name":"How do I pass data asynchronously to a view component in ASP.NET Core MVC?","acceptedAnswer":{"@type":"Answer","text":"Implement InvokeAsync in a class deriving from ViewComponent, injecting a service like IUserActivityService, calling its async method, and returning View(\"Default\", summary) with the fetched data as the model. Wrap the call in a try-catch to log errors and return a fallback Content(\"Unable to load activity metrics.\") result if the data fetch fails. Those building dashboards with async widgets can track ASP.NET Core view component patterns on daily.dev."}},{"@type":"Question","name":"How do I register custom tag helpers so they work across all Razor views in an ASP.NET Core MVC project?","acceptedAnswer":{"@type":"Answer","text":"Add an @addTagHelper directive in Views/_ViewImports.cshtml referencing your project's assembly, such as @addTagHelper *, YourProjectAssembly, alongside the built-in @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers directive. This makes every custom tag helper in that assembly available in all views without per-page imports. Developers setting up shared Razor tooling can keep ASP.NET Core configuration references on daily.dev."}}]}
```

