Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/24 9:02:01
.NET2.0App_Code文件夹的使用_[Asp.Net教程]

.NET2.0App_Code文件夹的使用_[Asp.Net教程]

在.NET2.0中,出现了App_Code文件夹。主要用途就是允许将代码进行动态编译(运行时编译) ,而不是象以前的版本那样,如果想引用类文件,必须通过DLL引用(必须编译生成好)。


另外该文件夹还有一个用途就是,允许放置多种语言编写的类文件(需要将其它语言的类文件放到子文件夹中)。这无疑在代码的共享上,提供了非常大的便利。下面就做一演示。


首先创建一个Web站点项目(C#)。


接下来添加一个C#类文件(CustomeClass.cs)到App_Code文件夹中。再添加一个子文件夹(Subdirectory)。


在Subdirectory下添加一个VB类文件(CommonClass.vb)。程序结构如图:



代码如下:


(1)CodeFolder2_cs.aspx.cs:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write(new part1().method1());
Response.Write(new Class1().GetMessage());

}
}


(2)CustomClass.cs:


public partial class part1
{
public part1()
{
//
// TOD 在此处添加构造函数逻辑
//
}
public string method1()
{
return "fhh";
}
}


(3)CustomClass.vb:


Public Class Class1
Public Function GetMessage() As String
Return "fhh"
End Function
End Class


(4)web.config代码:


<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="Subdirectory"/>
codeSubDirectories>
compilation>
system.web>
configuration>
(5)运行效果:



要注意:此例的web.config文件和页面文件(.aspx)与App_Code文件夹要在同一个级别上。


来源:sdtsfhh的blog







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759