August 09, 2015 by Anuraj
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>
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub