Search code examples
asp.net-mvchttpcontent-typeecmascript-5

How do I change the content type of static script files in ASP.NET MVC?


I have a standard ASP.NET MVC 5 web project, but I would like all the .js files within the Script folder to be served to the browser with the content type set to text/ecmascript instead of application/javascript.

The problem I have is the request is being sent to the server with:

GET /myapp/Scripts/test.js HTTP/1.1
Accept: text/ecmascript
...

But as .js files generally have a content type of application/javascript, the webserver responds with a 406 Not Accepted error.

How do I override the content type of static content in ASP.NET MVC?


Solution

  • I found the solution, simply add this to the web.config:

    <system.webServer>
      <staticContent>
        <remove fileExtension=".js"/>
        <mimeMap fileExtension=".js" mimeType="text/ecmascript"/>
      </staticContent>
    </system.webServer>