Last active
April 9, 2016 19:53
-
-
Save JamesRandall/1bd964fbd2af6375ee9ba096b9fa1cd7 to your computer and use it in GitHub Desktop.
Enable serving of static Markdown content to JavaScript running on any domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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