Search code examples
c#asp.netrazorrazorengine

Why HTML helper class is referred as Html instead of HtmlHelper?


I found this documentation for HtmlHelper class, thing that surprised me was, it was used in asp.net code as:

@Html.Action("Contact me","contact");

instead of

@HtmlHelper.Action("Contact me","contact");

As documentation has HtmlHelper as name of class then why we use it as Html only?


Solution

  • Similar to how the Model variable is available inside of your view - even though you haven't directly defined a Model variable yourself - Razor is generating a class that provides these instances of your model type and HtmlHelper in the scope of your cshtml file. It generates a method on that class which writes HTML according to the contents of your cshtml file.

    Model is an instance of the type specified by the @model directive, provided in the scope of your cshtml file when the generated method is called and having been passed as the argument to the View method in one of your actions.

    Html is an instance of type HtmlHelper, provided in the scope of your csthml file by the generated class.

    Url is an instance of type UrlHelper.