Convert markdown to html using asp.net core

1 minute read

Application overview
In this application, I will show how to create an application which is convert markdown to html using asp.net core. Let’s have a look on the implementation of the project.

Tools and Technology used The following tools and technologies has been used for this application

  • Visual Studio 2019
  • Visual C#
  • NuGet package “Markdig”
  • ASP.NET Core MVC
  • Razor view engine

Step1: Select ASP.NET Core Web Application
Select File->New->Project->ASP.NET Core Web Application

Step2: Choose project and solution name
Type project and solution name as “MarkdownToHtml”

Step 3: Select project template

  • Select project template as Web Application (Model-View-Controller)
  • Click create button

Step 4: Install Nuget package “Markdig”
Run the following command in package manager console

PM> Install-Package Markdig

Step 5: Add a method name Parse in MarkDownParser static class in Utility foler

MarkDownParser.cs

   public IActionResult Index()
    {
        string markdownText = "## This is a title of Markdown file ";
        string htmltext = MarkDownParser.Parse(markdownText);
        htmltext += MarkDownParser.Parse("  ") ;// for new line
        htmltext += MarkDownParser.Parse("__Strong text__");// for new line

        htmltext += MarkDownParser.Parse("  ");// for new line
        htmltext += MarkDownParser.Parse("* This is a bullet point");// bullet point
        ViewBag.HTMLText = htmltext;

        return View();
    }

Step 6: Modify Home->Index.cshtml as follows

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    @Html.Raw(ViewBag.HTMLText)
</div>

Step 7: Build and run the application

Now the application is ready to run. Build and run the application and watch the html output which you have given as markdown input in index action.

Source Code

Comments