Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning razor-parsed Javascript as a ViewResult from a controller
    primarykey
    data
    text
    <p>I've successfully created an mvc/razor web application that returns css files that have been parsed by razor. Each time there's a background-image I have a razor snippet that writes the URL prefix to the image file name. The CSS now looks like this:</p> <pre><code>body { background-image: url(@LookupUrl.Image("background.gif")); } </code></pre> <p>Css files now work fine and I've moved onto trying to get javascript .js files to function the same way but these aren't playing ball.</p> <p>The code is identical to the css code and it successfully finds the .js file, but razor seems to parse it differently. Here's an example js file:</p> <pre><code>function testFunction() { alert('test function hit!'); } testFunction(); </code></pre> <p>Razor seems to think it's code that it should compile, and gives the error:</p> <pre><code>Compiler Error Message: JS1135: Variable 'alert' has not been declared &gt; Source Error: &gt; &gt; Line 1: function testFunction() { &gt; Line 2: alert('test function &gt; hit!'); Line 3: } Line 4: &gt; testFunction(); </code></pre> <p>After renaming the same file to .css it works fine.</p> <p>Is there a way of getting razor to function with .js files in the same way as it does for .css? </p> <p>Here's how I registered the file handlers for razor:</p> <pre><code>RazorCodeLanguage.Languages.Add("js", new CSharpRazorCodeLanguage()); RazorCodeLanguage.Languages.Add("css", new CSharpRazorCodeLanguage()); WebPageHttpHandler.RegisterExtension(".js"); WebPageHttpHandler.RegisterExtension(".css"); </code></pre> <p>The build provider is registered in PreApplicationStart via the method Haacked outlines in <a href="http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx" rel="nofollow">his blog post</a>.</p> <p>Do I need to remove a handler that mvc adds for .js files?</p> <p><strong>UPDATE 2 days on</strong></p> <p>While I got working what I wanted to get working, I would not recommend this method to others. Using Razor to parse css/javascript is flawed without the use of &lt;text&gt;&lt;text/&gt; - it's the simplicity of razor using the @ ampersand that messes it up. Consider the CSS3 @font-face. Razor hits the @ and thinks it should use it as a function. The same thing can happen with javascript, and happened with Jquery 1.5.1. </p> <p>Instead, I'll probably go back to aspx webforms for dynamic css/javascript, where there's less chance of the &lt;% %> code blocks appearing naturally. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload