从控制器返回剃刀解析的Javascript作为ViewResult [英] Returning razor-parsed Javascript as a ViewResult from a controller
问题描述
我已经成功创建了一个mvc/razor Web应用程序,该应用程序返回由razor解析过的css文件.每次有背景图片时,我都有一个剃须刀片段,将URL前缀写到图像文件名. CSS现在看起来像这样:
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:
body { background-image: url(@LookupUrl.Image("background.gif")); }
Css文件现在可以正常工作了,我已经着手尝试使javascript .js文件以相同的方式起作用,但是它们并没有发挥作用.
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.
该代码与css代码相同,并且可以成功找到.js文件,但是razor似乎以不同的方式解析了该文件.这是一个示例js文件:
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:
function testFunction() { alert('test function hit!'); }
testFunction();
Razor似乎认为应该编译它的代码,并给出错误消息:
Razor seems to think it's code that it should compile, and gives the error:
Compiler Error Message: JS1135: Variable 'alert' has not been declared
> Source Error:
>
> Line 1: function testFunction() {
> Line 2: alert('test function
> hit!'); Line 3: } Line 4:
> testFunction();
将同一文件重命名为.css后,它可以正常工作.
After renaming the same file to .css it works fine.
是否有一种方法可以使剃须刀以.js文件的功能与.css的功能相同?
Is there a way of getting razor to function with .js files in the same way as it does for .css?
这是我为剃刀注册文件处理程序的方式:
Here's how I registered the file handlers for razor:
RazorCodeLanguage.Languages.Add("js", new CSharpRazorCodeLanguage());
RazorCodeLanguage.Languages.Add("css", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension(".js");
WebPageHttpHandler.RegisterExtension(".css");
Do I need to remove a handler that mvc adds for .js files?
更新2天
虽然我正在从事自己想从事的工作,但我不会向其他人推荐这种方法.在不使用< text>< text/>的情况下,使用Razor解析css/javascript是有缺陷的. -使用@&符号的剃须刀很简单,弄乱了它.考虑CSS3 @ font-face. Razor击中@并认为应将其用作函数. javascript和Jquery 1.5.1可能发生相同的情况.
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 <text><text/> - 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.
相反,我可能会回到动态css/javascript的aspx Webforms中,<%%>代码块自然出现的可能性较小.
Instead, I'll probably go back to aspx webforms for dynamic css/javascript, where there's less chance of the <% %> code blocks appearing naturally.
推荐答案
我不明白CSS为什么在JS不起作用的情况下起作用,特别是在复制粘贴的JS代码在CSS文件中起作用之后.
I couldn't understand why CSS worked while JS didn't, especially after the copy+pasted JS code worked inside the CSS file.
我在System.Web.WebPages.Razor源上的Visual Studio中使用了查找/替换对话框,以在项目中搜索字符串".js".那里没有任何帮助,因此我去了System.Web.WebPages项目.它在System.Web.WebPages.Util中找到了一个匹配项,它是一个带有一些辅助方法的静态类.
I used the find/replace dialogue within visual studio on the System.Web.WebPages.Razor source to search for the string '.js' within the project. There was nothing helpful there so I then went to the System.Web.WebPages project. It found a match in System.Web.WebPages.Util, which is a static class with a few helper methods.
其中一种方法是'EnsureValidPageType',并且在其中有一个try/catch块.在"catch"块中的注释:
One of those methods is 'EnsureValidPageType' and within there is a try/catch block. Inside the 'catch' block is a comment:
// If the path uses an extension registered with codedom, such as Foo.js,
// then an unfriendly compilation error might get thrown by the underlying compiler.
// Check if this is the case and throw a simpler error.
这让我相信.js带有一些特殊的内置处理程序.
It made me believe .js has got some special built-in handler with it.
我搜索了一下,找不到任何东西,然后查看了\ Windows \ Microsoft.NET \ Framework64 {version} \ Config中的web.config.
I googled for a bit, couldn't find anything, then looked in the web.config that's within \Windows\Microsoft.NET\Framework64{version}\Config.
其中有一个扩展名.js到
In there is a buildProvider mapping for the extension .js to
System.Web.Compilation.ForceCopyBuildProvider
在网站的web.config中删除此buildprovider后,.js文件将按预期进行编译和工作!
After removing this buildprovider in the website's web.config, .js files get compiled and work as they should!
我仍然不太清楚ForceCopyBuildProvider的用途或用途,但我想知道它是否适用于Visual Studio.各种扩展名都注册了不同的复制/忽略"构建提供者.
I'm still not too sure what the ForceCopyBuildProvider does or is for but I wonder if it's for visual studio. Various extensions have different Copy/Ignore build providers registered.
再次道歉回答我自己的问题,但我希望全面的答案可以帮助其他人.
Once again apologies for answering my own question but I hope the comprehensive(waffley) answer might help others out.
这篇关于从控制器返回剃刀解析的Javascript作为ViewResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!