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?
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With