[英]inline Server Code for C# Fails to work when moved to a JavaScript .js file
Within my C# code behind page, I have the following: 在页面后面的C#代码中,我具有以下内容:
MembershipUser currentUser = Membership.GetUser(false);
HttpContext.Current.Session["UserGuidAsString"] = currentUser.ProviderUserKey.ToString();
At the bottom of the corresponding ASPX page, I have the following code: 在相应的ASPX页面的底部,我有以下代码:
</form>
<script type="text/javascript"> var userGuidAsString = '<%=Session["UserGuidAsString"]%>';
alert(userGuidAsString);
</script>
</body>
</html>
It gives the proper expected result which is an alert box with the User's GUID when I run the page 它提供了适当的预期结果,当我运行页面时,这是带有用户GUID的警告框
I wanted to move the JavaScript code to a .js file so that the code is more modularized and organized. 我想将JavaScript代码移至.js文件,以便使代码更加模块化和更有条理。 I created the following test.js javascript file with the following contents: 我创建了以下具有以下内容的test.js javascript文件:
var userGuidAsString = '<%=Session["UserGuidAsString"]%>';
alert(userGuidAsString);
I also modified the ASPX page so that it would include the test.js javascript file: 我还修改了ASPX页面,使其包含test.js javascript文件:
</form>
<script src="/Scripts/test.js" type="text/javascript"></script>
</script>
</body>
</html>
It fails because it just gives an alert box with '<%=Session["UserGuidAsString"]%>' as a message 之所以失败,是因为它仅给出一个带有“ <%= Session [“ UserGuidAsString”]%>”的警报框
May I please know how I can make in line server C# code work within JavaScript .js files? 我是否可以知道如何使在线服务器C#代码在JavaScript .js文件中工作?
By This your accessing Server Variable 通过此访问服务器变量
'<%=Session["UserGuidAsString"]%>'
its not functionlity of Javascript. 它不是Javascript的功能。
You can write this code without js block also.. its like ASP code.. so its workg with .aspx page only not in javascript 您也可以在没有js块的情况下编写此代码。它类似于ASP代码..因此,它与.aspx页的工作方式仅不在javascript中
A common workaround for this is to put the server variable in an html hidden element. 常见的解决方法是将服务器变量放入html隐藏元素中。 For example: 例如:
<input type="hidden" id='guid' value='<%=Session["UserGuidAsString"]%>' />
Then your javascript file: 然后你的JavaScript文件:
var guid = document.getElementById("guid").value;
Or , you could define a js variable in your html, and reference that in your js file. 或者 ,您可以在html中定义js变量,然后在js文件中引用该变量。 Just make sure to declare the variable before you reference it. 只需确保在引用变量之前声明该变量即可。
<script type="text/javascript">
var guid = '<%=Session["UserGuidAsString"]%>';
</script>
<script type="text/javascript" src="yourScript.js></script>
Then in your js file you can simply use the guid
variable. 然后,在您的js文件中,您可以简单地使用guid
变量。
In order for C# to get executed on the server before the .js file reaches the client, you'd have to register that page extension in IIS so that the asp_net .dll processes .js files. 为了使C#在.js文件到达客户端之前在服务器上执行,您必须在IIS中注册该页面扩展名,以便asp_net .dll处理.js文件。
In your particular case (handling GUIDs), you really don't want to go this route, because with the nature of .js file caching, you're going to have a lot of problems ensuring that it's a fresh version being served up every time. 在您的特定情况下(处理GUID),您确实不想走这条路,因为由于.js文件缓存的性质,您将遇到很多问题,无法确保每个版本都提供新版本时间。
I suggest recreating a very simple (and yes, modular/reusable) JS function that makes a call to, say, GetUserGuidAsString.aspx by using AJAX or the equivalent. 我建议重新创建一个非常简单(是的,模块化/可重用)的JS函数,该函数通过使用AJAX或类似方法来调用GetUserGuidAsString.aspx。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.