I’ve been dabbling a bit with the new ASP.NET 5 TagHelpers and I was wondering how easy it would be to create one.
I’ve created a simple Markdown TagHelper with the CommonMark implementation.
So let me show you what it is, what each line of code is doing and how to implement it in an ASP.NET MVC 6 application.
The Code
1 | using CommonMark; |
Inspecting the code
Let’s start with the HtmlTargetElementAttribute
. This will wire the HTML Tag <markdown></markdown>
to be interpreted and processed by this class. There is nothing stop you from actually having more than one target.
You could for example target element <md></md>
by just adding [HtmlTargetElement("md")]
and it would support both tags without any other changes.
The Content
property will allow you to write code like this:
1 | @model MyClass |
This easily allows you to use your model or any server-side code without having to handle data mapping manually.
TagMode.SelfClosing will force the HTML to use self-closing tag rather than having content inside (which we’re not going to use anyway). So now we have this:
1 | <markdown content="Markdown" /> |
All the remaining lines of code are dedicated to making sure that the content we render is actual HTML. output.TagName
just make sure that we do not render the actual markdown
tag.
And… that’s it. Our code is complete.
Activating it
Now you can’t just go and create TagHelpers and have them automatically served without wiring one thing.
In your ASP.NET 5 projects, go to /Views/_ViewImports.cshtml
.
You should see something like this:
1 | @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers" |
This will load all TagHelpers from the Microsoft.AspNet.Mvc.TagHelpers
assembly.
Just duplicate the line and type-in your assembly name.
Then in your Razor code you can have the code bellow:
1 | public class MyClass |
1 | @model MyClass |
Which will output your markdown formatted as HTML.
Now whether you load your markdown from files, database or anywhere… you can have your user write rich text in any text box and have your application generate safe HTML.