Global.asax变量 [英] Global.asax Variables

查看:57
本文介绍了Global.asax变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我试图连接到Xml数据库,我想在Application_Start中加载Xml

文档。所以xml只加载一次,然后按照要求多次查询
,这应该是有效的方式来连接到数据库afaik,但是我可以'在
中访问xml对象我的aspx文件中出现此错误dbxml在当前上下文中不存在:


<%@应用程序语言= QUOT; C#" %>

< script runat =" server">

Xmldb dbxml;

void Application_Start(object sender,EventArgs e) {

dbxml = new Xmldb(Server.MapPath("〜"));

//在应用程序启动时运行的代码


} ...


我虽然在global.asax文件中声明的任何变量应该是

可用于任何其他aspx页面,如何我可以访问变量吗?


在此先感谢

Yehia A.Salam

Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml
Document in "Application_Start" so that the xml is loaded only once and then
queried later as many times as requested, this should the efficient way of
connecting to the database afaik, however I can''t access the xml object in
my aspx files with this error "dbxml does not exist in current context":

<%@ Application Language="C#" %>
<script runat="server">
Xmldb dbxml;
void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("~"));
// Code that runs on application startup

}...

I though that any variable declared in the global.asax files should be
available to any other aspx page, how could I access the variable?

Thanks In Advance
Yehia A.Salam

推荐答案

你在global.asax事件中定义的变量仍然只是那个

函数的本地变量,并且在函数结束时超出了范围。虽然文件

被称为global.asax,但其中的vars实际上并不是全局的。


在配置类或google上使用静态属性单身模式

了解更多关于如何执行此操作的信息。


Yehia A.Salam" < ye ***** @ hotmail.comwrote in message

news:48 ************************* ********* @ microsof t.com ...
Variables you define in the global.asax events are still only local to that
function and are out of scope when the function ends. IE although the file
is called global.asax, the vars in it are not actually global.

Use static properties on a configuration class or google "singleton pattern"
for more info on how to do this.

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:48**********************************@microsof t.com...

您好,


我正在尝试连接到Xml数据库,我想在Application_Start中加载

Xml文档。这样xml只加载一次

然后按要求多次查询,这应该是

有效的方式连接到数据库afaik,但我可以'不能访问

我的aspx文件中的xml对象有这个错误dbxml不存在于

当前上下文中:


<%@ Application Language =" C#" %>

< script runat =" server">

Xmldb dbxml;

void Application_Start(object sender,EventArgs e) {

dbxml = new Xmldb(Server.MapPath("〜"));

//在应用程序启动时运行的代码


} ...


我虽然在global.asax文件中声明的任何变量应该是

可用于任何其他aspx页面,如何我可以访问变量吗?


在此先感谢

Yehia A.Salam
Hello,

I was trying to connect to an Xml database, and I thought of loading the
Xml Document in "Application_Start" so that the xml is loaded only once
and then queried later as many times as requested, this should the
efficient way of connecting to the database afaik, however I can''t access
the xml object in my aspx files with this error "dbxml does not exist in
current context":

<%@ Application Language="C#" %>
<script runat="server">
Xmldb dbxml;
void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("~"));
// Code that runs on application startup

}...

I though that any variable declared in the global.asax files should be
available to any other aspx page, how could I access the variable?

Thanks In Advance
Yehia A.Salam






什么是Xmldb?


它是.Net Framework识别的对象吗?

你导入/声明了吗?它?


dbxml在当前上下文中不存在是一个相当明确的错误信息。


.Net Framework告诉你它不会识别Xmldb。

这就是你的原因' '无法创建dbxml作为Xmldb的实例。


Juan T. Llibre,asp.net MVP

asp.net faq : http://asp.net.do/faq/

foros de asp.net,en espa?ol: http:// asp。 net.do/foros/

================================ ======

" Yehia A.Salam" < ye ***** @ hotmail.comwrote in message

news:48 ************************* ********* @ microsof t.com ...
What is Xmldb ?

Is it an object recognized by the .Net Framework ?
Have you imported/declared it ?

"dbxml does not exist in current context" is a fairly clear error message.

The .Net Framework is telling you that it doesn''t recognize Xmldb.
That''s why you''re not able to create dbxml as an instance of Xmldb.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espa?ol : http://asp.net.do/foros/
======================================
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:48**********************************@microsof t.com...

您好,


我正在尝试连接到Xml数据库,我想在

Application_Start中加载Xml文档。所以xml只加载一次,然后多次查询请求

,这应该是连接到数据库afaik的有效方式,但是我不能

访问我的aspx文件中的xml对象时出现此错误dbxml在当前上下文中不存在:


<%@ Application Language =" C#" %>

< script runat =" server">

Xmldb dbxml;

void Application_Start(object sender,EventArgs e) {

dbxml = new Xmldb(Server.MapPath("〜"));

//在应用程序启动时运行的代码


} ...


我虽然在global.asax文件中声明的任何变量应该可用于任何其他aspx

页面,我可以访问该变量吗?


在此先感谢

Yehia A.Salam
Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml Document in
"Application_Start" so that the xml is loaded only once and then queried later as many times as
requested, this should the efficient way of connecting to the database afaik, however I can''t
access the xml object in my aspx files with this error "dbxml does not exist in current context":

<%@ Application Language="C#" %>
<script runat="server">
Xmldb dbxml;
void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("~"));
// Code that runs on application startup

}...

I though that any variable declared in the global.asax files should be available to any other aspx
page, how could I access the variable?

Thanks In Advance
Yehia A.Salam



首先,我很困惑dbxml = new Xmldb(Server.MapPath("〜")));

映射到某种对象?


但是假设你弄明白了,你可以试试:


public static Xmldb dbxml;


void Application_Start(object sender,EventArgs e){

dbxml = new Xmldb(Server.MapPath(" whatever));


}

此时,您应该可以从任何页面获取访问权限:


Global.dbxml;


- -

网站: http://www.eggheadcafe.com

UnBlog: http://petesbloggerama.blogspot.com

短网址&更多: http://ittyurl.net


Yehia A.Salam写道:
First, I''m confused as to how dbxml = new Xmldb(Server.MapPath("~"));
maps to some sort of object?

But assuming that you get that figured out, you could try:

public static Xmldb dbxml ;

void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("whatever"));

}

At this point, you should be able to gain access from any page with:

Global.dbxml;

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Yehia A.Salam" wrote:

您好,


我试图连接到Xml数据库,我想加载Xml

Application_Start中的文档所以xml只加载一次,然后按照要求多次查询
,这应该是有效的方式来连接到数据库afaik,但是我可以'在
中访问xml对象我的aspx文件中出现此错误dbxml在当前上下文中不存在:


<%@应用程序语言= QUOT; C#" %>

< script runat =" server">

Xmldb dbxml;

void Application_Start(object sender,EventArgs e) {

dbxml = new Xmldb(Server.MapPath("〜"));

//在应用程序启动时运行的代码


} ...


我虽然在global.asax文件中声明的任何变量应该是

可用于任何其他aspx页面,如何我可以访问变量吗?


在此先感谢

Yehia A.Salam
Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml
Document in "Application_Start" so that the xml is loaded only once and then
queried later as many times as requested, this should the efficient way of
connecting to the database afaik, however I can''t access the xml object in
my aspx files with this error "dbxml does not exist in current context":

<%@ Application Language="C#" %>
<script runat="server">
Xmldb dbxml;
void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("~"));
// Code that runs on application startup

}...

I though that any variable declared in the global.asax files should be
available to any other aspx page, how could I access the variable?

Thanks In Advance
Yehia A.Salam


这篇关于Global.asax变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆