C#网站 - 全球职能 [英] C# Website - Global Functions

查看:66
本文介绍了C#网站 - 全球职能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在VB .NET中编写网站时,我会经常将函数放在Global

中以供所有要调用的页面使用。现在,在C#中,这样做会导致引用

到非静态对象。等等。我意识到这意味着什么,

但我想知道的是什么是最好的方式呢?比如说,对于

的例子,我想要一个带有用户名和密码的函数和

如果成功登录则返回true或false,我想要任何页面或者

用户控制在网站上可以调用该功能,其中我应该把它放在哪里以及我需要什么语法?

问候,

David P. Donahue
dd******@ccs.neu .edu

When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references
to non-static objects" and whatnot. I realize what that means and all,
but what I''m wondering is what''s the best way around it? Say, for
example, I want a function that takes a username and a password and
returns true or false if it''s a successful login, and I want any page or
usercontrol in the website to be able to call that function, where
should I put it and what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu

推荐答案

您可以将您的方法添加到Global.asax,例如


public static bool登录()


{


私人布尔成功=假;


/ /在这里登录并设置成功


返回成功;


}


和称之为


bool loggedIn = Global.Login();


但我认为更好的解决方案是创建一个帮手上课并将

全局可用的方法放在那里上课。


HTH


DalePres

MCAD,MCDBA,MCSE

" ; David P. Donahue <峰; dd ****** @ ccs.neu.edu>在消息中写道

新闻:ON ************** @ TK2MSFTNGP12.phx.gbl ...
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
当我在网站上写VB .NET,我经常将函数放在Global
中以便调用所有页面。现在,在C#中,这样做会导致对非静态对象的引用。等等。我意识到这意味着什么,但是我想知道的是什么是最好的方式呢?比方说,我想要一个带有用户名和密码的函数,如果成功登录则返回true或
false,我想要
中的任何页面或用户控件网站能够调用该功能,我应该把它放在哪里?
我需要什么语法?

问候,
David P. Donahue
dd ****** @ ccs.neu.edu
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references to
non-static objects" and whatnot. I realize what that means and all, but
what I''m wondering is what''s the best way around it? Say, for example, I
want a function that takes a username and a password and returns true or
false if it''s a successful login, and I want any page or usercontrol in
the website to be able to call that function, where should I put it and
what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu



您建议的任何一种方法都可以使用。但是,如果这个全局代码中的某些内容引用了Session对象之类的内容,那该怎么办?相同的

非静态问题。我怎样才能参考正确的

会话?

问候,

David P. Donahue
dd ****** @ ccs.neu.edu


DalePres写道:
Either of the methods you suggest definitely work. However, what if
some of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
您可以将您的方法添加到Global.asax中,例如

public static bool登录()



私人布尔成功=假;

//在这里做一些登录并设置成功

返回成功;

}

bool loggedIn = Global.Login();

但是,我认为更好的解决方案是创建一个帮助类并放入
你希望在该课堂上全球可用的方法。

HTH

DalePres
MCAD,MCDBA,MCSE

" David P. Donahue <峰; dd ****** @ ccs.neu.edu>在消息中写道
新闻:ON ************** @ TK2MSFTNGP12.phx.gbl ...
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
当我写网站时VB .NET,我经常将函数放在Global
中以便调用所有页面。现在,在C#中,这样做会导致对非静态对象的引用。等等。我意识到这意味着什么,但是我想知道的是什么是最好的方式呢?比方说,我想要一个带有用户名和密码的函数,如果成功登录则返回true或
false,我想要
中的任何页面或用户控件网站能够调用该功能,我应该把它放在哪里?
我需要什么语法?

问候,
David P. Donahue
dd ** **** @ ccs.neu.edu
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references to
non-static objects" and whatnot. I realize what that means and all, but
what I''m wondering is what''s the best way around it? Say, for example, I
want a function that takes a username and a password and returns true or
false if it''s a successful login, and I want any page or usercontrol in
the website to be able to call that function, where should I put it and
what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu




将SessionID作为参数传递给全局方法。 />

在某些情况下,我必须创建

会话对象或SessionID字符串的应用程序范围集合,以便全局函数可以

跨会话沟通。如果你必须去那么远,有几个要记住的关键事项:


首先,将会话添加到Global的集合中。 Session_Start()

方法,然后确保从集合中删除会话从

Global.Session_End()方法。


其次,请注意授予一个会话所涉及的安全风险

访问能够识别并能够与另一个
会话进行通信的代码。 />

HTH


DalePres

" David P. Donahue" <峰; dd ****** @ ccs.neu.edu>在消息中写道

news:e1 **************** @ TK2MSFTNGP15.phx.gbl ...
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a couple
key things to remember:

First, add the session to the collection in from the Global.Session_Start()
method and then make sure you remove the session from your collection from
the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl...
其中一个你建议明确工作的方法。但是,如果这个全局代码的某些内容引用了Session对象之类的东西呢?同样的非静态问题。我怎样才能参考正确的会议?

问候,
David P. Donahue
dd ****** @ ccs.neu.edu

DalePres写道:
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
你可以添加你的Global.asax的方法如

public static bool登录()


私人bool成功=假;

//在这里做一些登录并设置成功

返回成功;

}

并称之为

bool loggedIn = Global.Login();

但是,我认为更好的解决方案是创建一个帮助器类并将
所需的方法放在该类中全局可用。

HTH

DalePres
MCAD,MCDBA,MCSE

" David P. Donahue" <峰; dd ****** @ ccs.neu.edu>在消息中写道
新闻:ON ************** @ TK2MSFTNGP12.phx.gbl ...
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
当我写网站时VB .NET,我经常将函数放在Global
中以便调用所有页面。现在,在C#中,这样做会导致对非静态对象的引用。等等。我意识到这意味着什么,但是我想知道的是什么是最好的方式呢?说,对于
示例,我想要一个带有用户名和密码的函数,如果登录成功则返回true或false,我想要任何页面或
用户控件网站能够调用该功能,我应该把它放在哪里以及我需要什么语法?

问候,
David P. Donahue
dd ** **** @ ccs.neu.edu
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references
to non-static objects" and whatnot. I realize what that means and all,
but what I''m wondering is what''s the best way around it? Say, for
example, I want a function that takes a username and a password and
returns true or false if it''s a successful login, and I want any page or
usercontrol in the website to be able to call that function, where should
I put it and what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu







这篇关于C#网站 - 全球职能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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