Create Custom HTML Helper in ASP.Net Core

前端 未结 7 1227
南笙
南笙 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:46

    This has been well explained by Danny van der Kraan in his blog post here. The answer below is an extract from this post:

    ASP.NET Core 1.0 [MVC 6] comes with a new exciting feature called TagHelpers. In ASP.Net Core 1.0 there is no concept of HTML Helper like in MVC.

    What are TagHelpers?

    TagHelpers can be seen as the evolution of HTML helpers which were introduced with the launch of the first MVC framework. To provide context you have to imagine that with classic ASP the only way you could automate the generation of HTML is via custom subroutines. After that ASP.NET came with server controls, with view states as biggest plus, to simulate the look and feel of desktop applications and help with the transition for desktop developers. But we all know what happens when we try to jam squares in to round holes. We had to face the fact that web development is nothing like desktop development. To get in line with proper web development the ASP.NET MVC framework was launched with HTML helpers to automate the HTML output. But HTML helpers never really gelled, especially not with front end developers and designers. One of the main pet peeves was that it made you switch a lot from angle brackets (HTML, CSS) to C# (Razor syntax) during work on views, which made the experience unnecessarily uncomfortable. [MVC 6] wants to address this and some smaller issues by introducing TagHelpers. Example HTML helper:

    @Html.ActionLink(”Home”, ”Index”, ”Home”)
    

    With the anchor TagHelper this would look like:

    <a asp-action="Index" asp-controller="Home">Home</a>
    

    PS: Please note that asp- is just a convention, but more on that later.

    The output rendered in the browser is the same for both:

    <a href="/">Home</a>
    

    PS: Provided the default route has not been altered.

    For more information about TagHelpers click here

    0 讨论(0)
提交回复
热议问题