在ASP.Net Core中创建自定义HTML帮助器 [英] Create Custom HTML Helper in ASP.Net Core
问题描述
我想创建自己的自定义HTML Helper,就像ASP.NET MVC中使用的HTML Helper,但是我找不到能够以正确方式实现它们的方法.
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.
我已经找到了如何创建自定义标签帮助器的方法,但没有创建HTML帮助器的方法.如何创建自己的自定义HTML帮助器?
I have found how to create custom Tag Helpers but not HTML Helpers. How do I create my own custom HTML Helpers?
推荐答案
对我来说,我认为我的HTML帮助器无法正常工作,直到我发现扩展方法现在位于IHtmlHelper
而不是HtmlHelper
.
For me I thought my HTML helpers weren't working until I spotted that the extension method is now on IHtmlHelper
not HtmlHelper
.
因此对于 .net核心:
public static IHtmlContent CheckboxListFor<TModel>(this IHtmlHelper<TModel> html,
Expression<Func<TModel, List<CheckboxListItem>>> expression) ...
代替 .net :
public static HtmlString CheckboxListFor<TModel>(this HtmlHelper<TModel> html,
Expression<Func<TModel, List<CheckboxListItem>>> expression) ...
编辑:我还已将.net core的返回类型更新为 IHtmlContent ,因为使用类似HtmlContentBuilder
的方法是编写HTML内容的一种更好的方法,返回IHtmlContent
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
这篇关于在ASP.Net Core中创建自定义HTML帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!