简体   繁体   English

如何在Javascript中访问Global.asax变量

[英]How to access Global.asax variables in Javascript

I am unsuccessful in accessing application state variable in javascript. 我无法在javascript中访问应用程序状态变量。

I declared the app state variable as, 我将应用状态变量声明为

Application["MYVAR"] ="My Variable";

JS: JS:

var _myjsvar = <%='Application["MYVAR"]'%>
alert(_myjsvar);

But I don't get the value in JS alert. 但是我没有在JS警报中获得该值。 Any help, appreciated. 任何帮助,不胜感激。

This should work as long as it is in a page executed by server ( ex :your aspx view etc..) 只要它在服务器执行的页面中(例如:您的aspx视图等),它就应该起作用。

var _myjsvar = '<%= Application["MYVAR"] %>';
alert(_myjsvar);

If it is a razor view where you want to access the Application variable, you can do this. 如果要在剃刀视图中访问Application变量,则可以执行此操作。

<script>
    var _myjsvar = "@HttpContext.Current.Application["MYVAR"]";
    alert(_myjsvar);
</script>

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

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