Skip to content

Instantly share code, notes, and snippets.

@xdumaine
Created March 31, 2014 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdumaine/9899202 to your computer and use it in GitHub Desktop.
Save xdumaine/9899202 to your computer and use it in GitHub Desktop.
A controller for serving arbitrary markdown documents from a directory, and having preprocessed keywords for writing.
public class DocumentationController : Controller
{
private const string DefaultDocPage = "Overview";
public ActionResult Index(string id = null)
{
ViewBag.HomeLinkPage = string.IsNullOrEmpty(id) || id == DefaultDocPage ? string.Empty : DefaultDocPage;
if (string.IsNullOrEmpty(ViewBag.HomeLinkPage))
{
id = DefaultDocPage;
}
var filePath = Server.MapPath(Url.Content("~/Content/Documentation/" + id.Trim("/".ToCharArray()) + ".md"));
if(!System.IO.File.Exists(filePath))
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return null;
}
var contents = new StringBuilder(System.IO.File.ReadAllText(filePath));
contents.Replace("{{AppRoot}}/", Url.Content("~"));
contents.Replace("{{Documentation}}", Url.Content("~/Documentation"));
contents.Replace("{{DocumentationImages}}", Url.Content("~/Content/Documentation/images"));
contents.Replace("{{SupportLink}}", ConfigurationManager.AppSettings["SupportLink"]);
return View("Index", (object)contents.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment