Posted by Anuraj on Sunday, August 9, 2015 Reading time :1 minute
Few days back I wrote a post on ASP.NET 5 Tag Helpers, this post is about building your own tag helper. As the first step you need to inherit from TagHelper class, which is available in “Microsoft.AspNet.Razor.Runtime.TagHelpers” namespace. TagHelper is an abstract class and contains virtual methods i.e. Process and ProcessAsync to facilitate both synchronous and asynchronous code execution that can be overridden in your custom Tag Helper class. ProcessAsync is the asynchronous method that we can use if we need to do some long operations. Both these methods has two parameters TagHelperContext and TagHelperOutput.
TagHelperContext gives you the complete information about all the attributes defined in the HTML tag where you define the Tag Helper attributes, and TagHelperOutput returns the output generated by your Tag Helper. You can add attributes with [HtmlAttributeName] attribute, which will help to specify attributes to the HTML element.
Here is the sample ajax post implementation of tag helper, which an attribute for Form element. This code is using JQuery post method.
And you can include this tag helper in _ViewImports.cshtml file. Or you can add @addTagHelper “*,
You can enable ajax in form element like this.
Hope it helps. Happy Programming :)</assemblyname>
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.