Skip to content

Instantly share code, notes, and snippets.

@JamesRandall
Last active April 9, 2016 19:53
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 JamesRandall/1bd964fbd2af6375ee9ba096b9fa1cd7 to your computer and use it in GitHub Desktop.
Save JamesRandall/1bd964fbd2af6375ee9ba096b9fa1cd7 to your computer and use it in GitHub Desktop.
Enable serving of static Markdown content to JavaScript running on any domain
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyWebsite.Helpers
{
public class AllowMarkdownCorsModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequestHandler;
}
private void BeginRequestHandler(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
if (fileExtension?.ToLower() == ".md")
{
context.Response.AddHeader("access-control-allow-origin", context.Request.Headers["origin"]);
if (context.Request.Headers["access-control-request-headers"] != null)
{
context.Response.AddHeader("access-control-allow-headers", context.Request.Headers["access-control-request-headers"]);
}
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(3600));
context.Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(3600));
}
}
public void Dispose()
{
}
}
}
<configuration>
<system.webServer>
<modules>
<add name="MarkdownAllowCorsModule" type="MyWebsite.Helpers.AllowMarkdownCorsModule" />
</modules>
<staticContent>
<remove fileExtension=".md" />
<mimeMap fileExtension=".md" mimeType="text/markdown" />
</staticContent>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment