简体   繁体   English

在MVC框架中使用Javascript文件内的Inline C#

[英]Using Inline C# inside Javascript File in MVC Framework

I am trying to get inline C# to work in my JavaScript files using the MVC Framework. 我正在尝试使用MVC框架使内联C#在我的JavaScript文件中工作。 I made this little test code up. 我制作了这个小测试代码。

$(document).ready(function() {
    alert(<%= ViewData["Message"] %>);
});

When this code is used inside of the view it works perfectly. 当在视图内部使用此代码时,它可以完美地工作。 When I get outside of my aspx view and try this in a JavaScript file I get illegal XML character. 当我离开我的aspx视图并在JavaScript文件中尝试这个时,我得到了非法的XML字符。 I figure this is by design in the MVC Framework but I haven't been able to find any material on this on the web. 我认为这是MVC框架中的设计,但我无法在网上找到任何相关材料。

Has anyone gotten inline C# to work in JavaScript files using the MVC Framework? 有没有人使用MVC框架让内联C#在JavaScript文件中工作?

As others have said, the C# is not being processed by the server. 正如其他人所说,服务器没有处理C#。

A possible solution would be to have a separate view that uses the same model and outputs the JavaScript, then reference that view in your <script type="text/javascript" src="yourJSview.aspx"></script> . 一个可能的解决方案是使用一个单独的视图,使用相同的模型并输出JavaScript,然后在<script type="text/javascript" src="yourJSview.aspx"></script>引用该视图。

Added as per SLaks' answer : 根据SLaks的回答添加:

Set the content-type to text/javascript , and put your JavaScript source directly below the <%@ Page directive (without a <script> tag). 将content-type设置为text/javascript ,并将JavaScript源直接放在<%@ Page指令下(不带<script>标记)。

Beware that you won't get any IntelliSense for it in VS. 请注意,您不会在VS中获得任何IntelliSense。

.aspx files are the view files of MVC framework. .aspx文件是MVC框架的视图文件。 The framework only renders the views and partial views. 该框架仅呈现视图和部分视图。 I do not think there would exist a way to use server-side code inside javascript files. 我认为不存在在javascript文件中使用服务器端代码的方法。

You can include your message in a hidden field 您可以在隐藏字段中包含您的消息

<%-- This goes into a view in an .aspx --%>
<%= Html.Hidden("MyMessage", ViewData["Message"]) %>

and use that inside your javascript file: 并在你的javascript文件中使用它:

// This is the js file
$(document).ready(function() {
    alert($("#MyMessage").attr("value"));
});

You could make an ASPX view that renders JavaScript. 您可以创建一个呈现JavaScript的ASPX视图。

Set the content-type to text/javascript , and put your JavaScript source directly below the <%@ Page directive (without a <script> tag). 将content-type设置为text/javascript ,并将JavaScript源直接放在<%@ Page指令下(不带<script>标记)。

Beware that you won't get any IntelliSense for it in VS. 请注意,您不会在VS中获得任何IntelliSense。

To add to Grant Wagner's answer and SLaks's answer , you can actually fool Visual Studio into giving you IntelliSense in his solution like so: 为了增加Grant Wagner的答案SLaks的答案 ,你实际上可以欺骗Visual Studio在他的解决方案中为你提供IntelliSense:

<%if (false) {%><script type="text/javascript"><%} %>
// your javascript here
<%if (false) {%></script><%} %>

In his solution it is required that when it renders to the page that there be no <script> tags, but that has a side effect that turns off JavaScript IntelliSense in Visual Studio. 在他的解决方案中,当它向页面呈现没有<script>标签时,要求它具有在Visual Studio中关闭JavaScript IntelliSense的副作用。 With the above, Visual Studio will give you IntelliSense, and at the same time not render the <script> tags when executed. 有了上述内容,Visual Studio将为您提供IntelliSense,同时在执行时不会呈现<script>标记。

That inline C# has to be processed by the server in order to make sense. 必须由服务器处理内联C#才有意义。 Naturally, it won't work with a just-plain-JavaScript file. 当然,它不适用于普通的JavaScript文件。

Your web server does not process .js files, it only serves them to the client. 您的Web服务器不处理.js文件,它只为客户端提供服务。 This is in contrast to .aspx or other ASP.NET file types. 这与.aspx或其他ASP.NET文件类型形成对比。 These files are interpreted by your server before they are served up to the client. 这些文件在服务器提供给客户端之前由服务器解释。

This is an old question, but for those who stumble upon in through google in the future, the best solution is to use data-* attributes to pass in the variables. 这是一个老问题,但对于那些在将来通过谷歌偶然发现的人来说,最好的解决方案是使用data- *属性来传递变量。 A hidden element could be used, but you might as well use the <script> tag itself, and give it a unique ID. 可以使用隐藏元素,但您也可以使用<script>标记本身,并为其指定唯一ID。

A full example is answered here: https://stackoverflow.com/a/18993844/1445356 这里有一个完整的例子: https//stackoverflow.com/a/18993844/1445356

when you have C# code in a separate file and include it in your View the Server does not process the code, the script file will be called by the browser and the inline script would be treated as plain string 当您在单独的文件中包含C#代码并将其包含在View中时,服务器不会处理代码,浏览器将调用脚本文件,并将内联脚本视为纯字符串

alternatively you can try script runat=server when including you script file but I am not sure about the effects of this 或者你可以在包含脚本文件时尝试脚本runat = server,但我不确定这会产生什么影响

I agree with Serhat. 我同意Serhat的观点。 It's best to render an HTML Hidden field, or, as Al mentioned, go to a URL for it. 最好呈现一个HTML隐藏字段,或者,正如Al所提到的,转到它的URL。 This can be done through a Web Service or even an IHttpHandler implementation. 这可以通过Web服务甚至IHttpHandler实现来完成。 Then you could use a url such as "messages.axd?id=17". 然后你可以使用诸如“messages.axd?id = 17”之类的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM