“全球变量”的最佳位置和ASP.Net中的C#方法 [英] Best place for "Global Variables" and Methods in C# though ASP.Net

查看:76
本文介绍了“全球变量”的最佳位置和ASP.Net中的C#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个月前,我终于开始学习C#和

ASP.Net,来自经典的ASP和VBScript背景。


In我的经典ASP,我有自己的小代码库,我把它放在了一个名为Common_VBscript.asp的包含文件的
中。它有我用过的所有

常见的东西。 (常量,连接字符串,小小的功能

类似于电子邮件验证功能,或用于准备字符串的功能,用于

db条目等)


对我来说,拥有公共代码的最佳方法是什么?对于特定项目的所有(或许多)ASP.Net页面,模块是什么?
?并且

我该怎么办呢,确切地说?


谢谢,

BP

解决方案

< Bu ********* @ gmail.comwrote in message

news:11 ****** ****************@e34g2000pro.googlegr oups.com ...


>一个月前我终于服用了暴跌并开始学习C#和

ASP.Net,来自经典的ASP和VBScript背景。


在我的经典ASP中,我有自己的小东西我插入的代码库

一个名为Common_VBscript.asp的包含文件它有我用过的所有

常见的东西。 (常量,连接字符串,小小的功能

类似于电子邮件验证功能,或用于准备字符串的功能,用于

db条目等)


对我来说,拥有公共代码的最佳方法是什么?对于特定项目的所有(或许多)ASP.Net页面,模块是什么?
?并且

我怎么能这样做呢?



ASP.NET与.NET Framework的其余部分一样,是面向对象的。

一般来说,如果你是寻找全球的什么,你可能

需要对OOP进行一些复习...


一个显而易见的解决方案是创建一个具有常用功能的新类。然后,

,只要你需要一个或多个,只需实例化该类并使用

你需要的功能......

此外,C#支持使用静态方法的类,您可以在代码中的任何地方使用

而无需实例化类。纯粹主义者可能会这样做,但这可能很有用:
http://www.eggheadcafe.com/articles/20020206.asp

而且,纯粹为了平衡,VB.NET可以用

做同样的事情它的共享方法...


但是,要非常警惕静态变量,因为它们在所有的

会话,这可能会导致一些非常意外和不需要的结果...

-

Mark Rae

ASP .NET MVP
http://www.markrae.net


另外使用web.config进行加密连接字符串和其他

配置,还可以使用XML配置文件等。


可能希望从某些基础继承一些aspx页面并使用常见的

功能。


" Mar k Rae [MVP]" < ma ** @ markNOSPAMrae.netwrote in message

news:eL ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
< Bu ********* @ gmail.comwrote in message

news:11 ********** ************@e34g2000pro.googlegr oups.com ...


>>一个月前我终于冒险了并开始学习C#和
ASP.Net,来自经典的ASP和VBScript背景。

在我的经典ASP中,我有自己的小代码库,我被困在
一个名为Common_VBscript.asp的包含文件。它有我用过的所有常见的东西。 (常量,连接字符串,小功能
像电子邮件验证功能,或用于准备字符串进行数据库输入的功能等)

什么是最好的方法我有一个公共代码对于特定项目,所有(或许多)ASP.Net页面上的模块都已添加到了?
我怎么能这样做呢?



ASP.NET与.NET Framework的其余部分一样,是面向对象的。

一般来说,如果你是寻找全球的什么,你可能

需要对OOP进行一些复习...


一个显而易见的解决方案是创建一个具有常用功能的新类。然后,

,只要你需要一个或多个,只需实例化该类并使用

你需要的功能......

此外,C#支持使用静态方法的类,您可以在代码中的任何地方使用

而无需实例化类。纯粹主义者在这样做时可能会退缩

,但这可能很有用:
http://www.eggheadcafe.com/articles/20020206.asp

而且,纯粹为了平衡,VB.NET可以用

做同样的事情它的共享方法...


但是,要非常警惕静态变量,因为它们会在

所有会话,这可能会导致一些非常意外和不必要的

结果...


-

Mark Rae

ASP.NET MVP
http:// www .markrae.net



谢谢你的回复!根据您的意见提出几个问题

如下:


一个明显的解决方案是创建一个具有常用功能的新类。然后,

,只要你需要一个或多个,只需实例化该类并使用

你需要的函数...


是的,这听起来很棒,也是我的想法。麻烦的是,

而我理解如何制作Web表格。 (ASPX文件)和网络用户

控制 (ASCX文件),制作一个单独的类,并且能够

包括它只在所需的页面中逃脱我。在Visual Studio中,我可以使用
查看make class。当添加一个新项目,但它想把它坚持到

App_Code,我认为这不是我想要的地方。 (这将使

我在整个应用程序中使用的静态变量,

对吗?有很多次我使用变量我只想要

常见特定会话。)


有人可以给我明确指示如何包含我已经制作的类别(a b $ b)文件名为Common.cs,例如在特定页面中

使用它包含的方法和变量,而不使用

App_Code或_SHOULD_我正在使用" App_Code文件"为此?


谢谢,

B.Paulson


A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?

Thanks,
B.P.

解决方案

<Bu*********@gmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...

>A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?

ASP.NET, like the rest of the .NET Framework, is object-orientated.
Generally speaking, if you''re looking for "global" anything, you probably
need a bit of a refresher on OOP...

An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...

In addition, C# supports classes with static methods which you can use
anywhere in your code without instantiating the class. Purists may flinch at
doing this, but it can be useful:
http://www.eggheadcafe.com/articles/20020206.asp

And, purely in the interests of balance, VB.NET can do the same thing with
its Shared methods...

However, be very wary of static variables, as they will be common across all
sessions, which can lead to some very unexpected and unwanted results...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


in addition use the web.config for encrypted connection string and other
configurations, may additionally use XML config files etc..

may want to have some aspx pages inherit from some base with common
functionality.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eL**************@TK2MSFTNGP04.phx.gbl...

<Bu*********@gmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...

>>A month ago I finally took the plunge and began learning C# and
ASP.Net, coming from a Classic ASP and VBScript background.

In my classic ASP, I had my own little library of code that I stuck in
an include file called "Common_VBscript.asp" which had all of the
common stuff I used. (Constants, Connection Strings, little utlities
like "email validation" functions, or functions to ready strings for
db entry, etc.)

What is the best way for me to have a "common code" module tacked on
to all (or many) of my ASP.Net pages for a particular project? And
how can I do it, precisely?


ASP.NET, like the rest of the .NET Framework, is object-orientated.
Generally speaking, if you''re looking for "global" anything, you probably
need a bit of a refresher on OOP...

An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...

In addition, C# supports classes with static methods which you can use
anywhere in your code without instantiating the class. Purists may flinch
at doing this, but it can be useful:
http://www.eggheadcafe.com/articles/20020206.asp

And, purely in the interests of balance, VB.NET can do the same thing with
its Shared methods...

However, be very wary of static variables, as they will be common across
all sessions, which can lead to some very unexpected and unwanted
results...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net



Thank you for the reply! A few questions based on your comments
follow:

An obvious solution is to create a new class with common functions. Then,
whenever you need one or more of them, just instantiate the class and use
the function(s) you need...

Yes, that sounds terrific, and is what I had in mind. Trouble is,
while I understand how to make "Web Forms" (ASPX files) and "Web User
Controls" (ASCX files), making a separate class and being able to
include it only in needed pages escapes me. In Visual Studio, I can
see "make class" when adding a new item, but it wants to stick it into
"App_Code", which I do not think is where I want it. (This will make
whatever static variables I use common across the entire application,
right? There are many times where I am using variables I only want
"common" to particular sessions.)

Can someone give me explicit directions on how to include a Class that
I have made (a file called "Common.cs) for instance in specific pages
that use methods and variables that it includes, without using
"App_Code", or _SHOULD_ I be using "App_Code" for this?

Thanks,
B.Paulson


这篇关于“全球变量”的最佳位置和ASP.Net中的C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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