Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Return Markdown Directly From Your ASP.NET MVC Controller

DZone's Guide to

Return Markdown Directly From Your ASP.NET MVC Controller

Facilitating the use of Markdown for static content in you MVC solutions using MarkdownWeb in a few lines of code.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Want to return Markdown directly from your ASP.NET MVC controller? Here is how you do it.

You can use my MarkdownWeb nuget package. It uses the MarkdownDeep package for the Markdown parsing, but adds features such as github tables syntax and fenced code blocks.

To use it, simply create a new Markdown file and add it to a view folder:

views folder

View contents:

# Hello world!

This is some tough shit.

## Table

Table demo

 Column 1 | Column 2 
----- | ------
Value 1 | Value A
Value 2 | Value B


Then call it from your controller:

public ActionResult Info()
{
    return new MarkdownResult("info");
}


Result:

markdown result

The magic is done thanks to my nuget package and the following action result:

public class MarkdownResult : ActionResult
{
    private readonly string _fileName;
    private readonly string _markdownFullPath;

    public MarkdownResult(string virtualPathOrMarkdownViewName)
    {
        if (virtualPathOrMarkdownViewName == null)
            throw new ArgumentNullException(nameof(virtualPathOrMarkdownViewName));
        if (virtualPathOrMarkdownViewName.StartsWith("~"))
            _markdownFullPath = HostingEnvironment.MapPath(virtualPathOrMarkdownViewName);
        else
            _fileName = virtualPathOrMarkdownViewName;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        string fullPath;
        if (_fileName != null)
        {
            var fullName = string.Format("~\\Views\\{0}\\{1}",
                context.Controller.GetType().Name.Replace("Controller", ""), _fileName);
            fullPath = HostingEnvironment.MapPath(fullName);
        }
        else
        {
            fullPath = _markdownFullPath;
        }
        if (!fullPath.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
            fullPath += ".md";

        var path = Path.GetDirectoryName(fullPath);
        var repos = new FileBasedRepository(path);
        var parser = new PageService(repos, new UrlConverter("/"));
        var fileContents = File.ReadAllText(fullPath);
        var result = parser.ParseString("/", fileContents);
        context.HttpContext.Response.Write(result.Body);
    }
}


Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
markdown ,asp.net-mvc

Published at DZone with permission of Jonas Gauffin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}