Create Custom HTML Helper in ASP.Net Core

前端 未结 7 1378
南笙
南笙 2020-12-28 13:10

I want to create my own custom HTML Helper like the ones used in ASP.NET MVC, but I haven\'t been able to find how to implement them in the correct way.

I have found

7条回答
  •  梦谈多话
    2020-12-28 13:41

    For me I thought my HTML helpers weren't working until I spotted that the extension method is now on IHtmlHelper not HtmlHelper.

    So for .net core:

    public static IHtmlContent CheckboxListFor(this IHtmlHelper html,
                Expression>> expression) ...
    

    Instead of for .net:

    public static HtmlString CheckboxListFor(this HtmlHelper html,
                Expression>> expression) ...
    

    EDIT: I've also updated the return type for .net core to be IHtmlContent as using something like HtmlContentBuilder is a nicer way to compose HTML content and returning that returns IHtmlContent

提交回复
热议问题