Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Razor find my HTML Helper?

I'm trying to port a "classic" ASP.NET MVC view to Razor and got stuck when trying to use a traditional (non-Razor) Html helper method. The helper method has the following signature:

public static string WrappedValidationSummary(this HtmlHelper htmlHelper, string SummaryError)
{
...
}

The helper method does work fine when using it in regular (non-Razor) views.

When using it in the Razor view like this:

@Html.WrappedValidationSummary("Mitarbeiter konnnte nicht angelegt werden.");

I get a run-time error message that

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'WrappedValidationSummary' and no extension method 'WrappedValidationSummary' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

The Razor syntax checker in Visual Studio and Intellisense have no problem finding my extension method's definition. Recompiling the project does not help.

What is going wrong?

like image 607
Adrian Grigore Avatar asked Dec 15 '10 12:12

Adrian Grigore


People also ask

Should I use tag helpers or HTML helpers?

Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

Where do we use HTML helpers?

An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.

What is HTML tag helper?

A Tag Helper Component is a Tag Helper that allows you to conditionally modify or add HTML elements from server-side code. This feature is available in ASP.NET Core 2.0 or later. ASP.NET Core includes two built-in Tag Helper Components: head and body . They're located in the Microsoft. AspNetCore.


1 Answers

Have you added the helper's namespace to your Views/web.config?

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="CUSTOM_NAMESPACE" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

The above will only work if you're using an RC, if you're on an early Beta, you'll need to add the namespace in the page or Global.asax.

Also, I'd suggest changing the return type to HtmlString.

return new HtmlString(STRING_VALUE);
like image 51
kim3er Avatar answered Nov 23 '22 17:11

kim3er