0

Suppose I want to render javascript from an external file inline into my layout file for performance reasons.

If I use the following Razor code

<script>
    @RenderPage("~/Content/my.js")
</script>

results in

Server Error in '/' Application.

The following file could not be rendered because its extension ".js" might not be supported: "~/Content/my.js".

If I merely rename my javascript file to my.js.cshtml

<script>
    @RenderPage("~/Content/my.js.cshtml")
</script>

The peasants rejoice.

The question:

Is there any simple way for me to prevent RenderPage from nagging such that I can tell it that the .js extension is fine?


With some feedback from @choudeshell one potential solution is:

<compilation debug="true" targetFramework="4.5">
  <buildProviders>
    <remove extension=".js"/>
    <add extension=".js" type="System.Web.WebPages.Razor.RazorBuildProvider,
                               System.Web.WebPages.Razor"/>
  </buildProviders>  

</compilation>

What type of side effects would there be from removing whatever the default is for .js, any?

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • And yes I know what I'm doing, I know about bundling/minification/caching. For illustration purposes you could view my.js to be an asynchronous scriptloader. – Chris Marisic Aug 08 '14 at 13:57
  • 1
    This can help: http://stackoverflow.com/questions/5925679/inline-javascript-in-my-razor-file-can-i-have-this-included-from-an-external – bob Aug 08 '14 at 14:09
  • @bob I had thought of reading it off disk myself but I'd much rather leverage the built in RenderPage that I'm sure operates in a less naive fashion than anything I would write for IO. That question does specifically mention doing .cshtml since my question is about avoiding renaming the file I would say this question does not duplicate that link. – Chris Marisic Aug 08 '14 at 14:14

0 Answers0