简体   繁体   English

将服务器端代码嵌入单独的JavaScript文件中

[英]Embed server side code in the separate JavaScript files

Imagine you have an application that consists C# .NET code for the server side, and JavaScript files on the client side. 假设您有一个包含服务器端C#.NET代码和客户端JavaScript文件的应用程序。 As you know you can embed server side code by using <% %> tags, or other tags like @ id you are using MVC razor view engine. 如您所知,您可以使用<% %>标签或其他标签(如@id)嵌入服务器端代码,而您正在使用MVC剃刀视图引擎。

Now imagine you have separate JavaScript files that are loaded by the script tag in the head tag: how can I embed server side code into the code in the JavaScript files? 现在,假设您有一个单独的JavaScript文件,这些文件由head标签中的script标签加载:如何将服务器端代码嵌入到JavaScript文件中的代码中? Is there a way to do this? 有没有办法做到这一点?

Send the header "Content-type: text/javascript" and load it as 发送标题“ Content-type:text / javascript”,并将其加载为

<script type="text/javascript" src="whatever.aspx?whatever=someparm"></script>

For example 例如

Response.ContentType = "text/javascript"; Response.ContentType =“文本/ javascript”;

or 要么

Response.Headers.Add("Content-type", "text/javascript"); Response.Headers.Add(“ Content-type”,“ text / javascript”);

Perhaps you can do this: How to pass variables in to a JS file 也许您可以这样做: 如何将变量传递到JS文件中

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

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