Search code examples
asp.net-corerazor-pagestag-helpersasp.net-core-tag-helpers

How to access Razor Page object in TagHelper


I am writing a taghelper for ASP.NET Core 3.1 Razor Pages applications. In my TagHelper I would like to access the Razor Page object (in purpose to set Layout = null conditionally)

I do know the

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

facility, but the Page is not seems to accessible via ViewContext.

Question

Do I have to pass the Page itself explicitly via a TagHelper property, or is there any more convenient way, not to force the user of my TagHelper repeatedly add this extra attribute?


Solution

  • ViewContext.ViewData.Model
    

    This returns an object, just cast it to the type you want to access its properties.