将经典 ASP Javascript 包含升级到 .net
我正在开发一个 ASP.net 3.5 项目,该项目是从经典 ASP 项目发展而来的。我们有两个旧的 Javascript 文件,它们包含在几个页面中,并且从未正确更新过。 Javascript 包含 ASP.Net 服务器标记,文件以 .js.aspx 扩展名保存。
嵌入的 VB.net 引用类中的共享(静态)属性以及 ASP.net 会话变量。有问题的类未导入 Javascript 文件中,因此 Visual Studio 无法编译页面。但是,当您加载包含这些文件的页面时,它可以正常工作。
将这些文件更新为 .Net 标准的正确方法是什么?我们是否应该避免在 Javascript 文件中包含服务器标签,或者是否有一种 .Net 方法可以将 Javascript 和服务器端代码混合在一个可包含的文件中?
谢谢。
编辑以回答问题:
该文件包含在 中在脚本标签内。编译器错误是:“名称'MyClass'未声明”,其中MyClass是被引用的类。
I'm working on an ASP.net 3.5 project that has evolved out of a Classic ASP project. We have two old Javascript files that are included in a few pages and have never been updated properly. The Javascript contains ASP.Net server tags and the files are saved with a .js.aspx extension.
The embedded VB.net refers to shared (static) properties in a class and also the ASP.net Session variable. The class in question isn't imported in the Javascript files, so Visual Studio fails to compile the page. However, when you load a page that includes these files, it works fine.
What is the correct way to update these files to .Net standards? Should we avoid having server tags in Javascript files or is there a .Net way to mingle Javascript and server-side code in an includable file?
Thanks.
Edit to answer question:
The file is being included with <!-- #include virtual="myFile.js.aspx" --> inside a script tag. The compiler error is: "Name 'MyClass' is not declared", where MyClass is the class being referenced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你需要 JS 函数中的服务器端数据,我会建议不同的方式。
例如,假设您现在有这样的事情:
将其更改为这样:
然后在页面本身的
Page_Load
事件中添加这样的代码:这将很好地发送值,在我看来比直接在 JS 代码中更正确。
If you need server side data inside the JS functions, I would suggest different way.
For example, suppose you now have such a thing:
Change this to be this instead:
Then in the
Page_Load
event of the page itself add such code:This will send the value just fine, and in my opinion is more correct than having it directly in the JS code.
完全限定您的类,例如,如果您的类位于名称空间中
然后在 .js.aspx 文件中执行
YourCompany.YourApp.MyClass
并确保已添加对其所在 DLL 的引用。
另请注意,ASP 样式包含在 ASP.Net 中不起作用。
Fully qualify your class , for example if your class is in the namespace
then in your .js.aspx file you do
YourCompany.YourApp.MyClass
And make sure you have added reference to the DLL where this resides.
Also note that ASP style includes do not work in ASP.Net.