Building a Web Application->Sharing Code Between Pages
Although you can place code inside //在...里面each page within your site//在站点里 (using the inline or code-behind separation models described in the previous section//前一部分), there are times when you will want to share code //共享代码across several pages //通过几个页面in your site. It would be inefficient //低效率的and difficult to maintain//维护 this code by copying it to every page //复制到每个页面中that needs it. Fortunately//幸运地, ASP.NET provides several convenient//方便的 ways to make code accessible//可得到的,易接近的,可进入的 to all pages//所有的页面 in an application.
其中:CodeFolder_cs.aspx
<%@ page language="C#" %>

<script runat="server">

void Button1_Click(object sender, EventArgs e)
{
CustomClass c = new CustomClass();
Label1.Text = c.GetMessage(TextBox1.Text);
}
</script>

<html>
<head>
<title>ASP.NET Inline Pages</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Text="Hello" Runat="server" />
</form>
</body>
</html>
CustomClass.cs
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;

public class CustomClass
{
public String GetMessage(String input) {
return "Hello " + input;
}
}
By default,//默认情况下. the App_Code directory can only contain files of the same language. However, you may partition//分割开 the App_Code directory into subdirectories//子目录 (each containing files of the same language) in order to contain multiple languages under the App_Code directory. To do this, you need to register each subdirectory //子目录in the Web.config file for the application.
<%@ page language="C#" %>

<script runat="server">

void Button1_Click(object sender, EventArgs e)
{
CustomClass c = new CustomClass();
Label1.Text = c.GetMessage(TextBox1.Text);

CustomClass2 c2 = new CustomClass2();
Label2.Text = c2.GetMessage(TextBox1.Text);
}
</script>

<html>
<head>
<title>ASP.NET Inline Pages</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Runat="server" />
<br />
<asp:Label ID="Label2" Runat="server" />
</form>
</body>
</html>
<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="Subdirectory"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;

public class CustomClass
{
public String GetMessage(String input) {
return "Hello " + input;
}
}
'-----------------------------------------------------------------------
' This file is part of the Microsoft .NET SDK Code Samples.
'
' Copyright (C) Microsoft Corporation. All rights reserved.
'
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation. See these other
'materials for detailed information regarding Microsoft code samples.
'
'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'-----------------------------------------------------------------------
Imports Microsoft.VisualBasic

Public Class CustomClass2

Public Function GetMessage(ByVal name As String) As String
Return "Hello from VB " & name
End Function

End Class
The Code Directory //代码目录New in 2.0
Just as pages can be compiled dynamically //动态编译at runtime, so can arbitrary //任意的,恣意的,专制的code files (for example .cs or .vb files). ASP.NET 2.0 introduces the App_Code directory, which can contain standalone//独立的,单独的 files that contain code to be shared across several pages//几个页面共享 in your application. Unlike ASP.NET 1.x, which required these files to be precompiled //预编译to the Bin directory//二进制目录, any code files in the App_Code directory will be dynamically compiled //动态编译at runtime and made available to//成为可用 the application. It is possible to place files of more than one language under the App_Code directory, provided they are partitioned//分割开 in subdirectories//子目录 (registered with a particular language in Web.config). The example below demonstrates using the App_Code directory to contain a single class file //类文件called //调用from the page.C# App_Code Folder Example
文件夹层次显示如下:

文件夹层次显示如下:
其中:CodeFolder_cs.aspx
<%@ page language="C#" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
CustomClass c = new CustomClass();
Label1.Text = c.GetMessage(TextBox1.Text);
}
</script>
<html>
<head>
<title>ASP.NET Inline Pages</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Text="Hello" Runat="server" />
</form>
</body>
</html>
CustomClass.cs
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
public class CustomClass
{
public String GetMessage(String input) {
return "Hello " + input;
}
}
By default,//默认情况下. the App_Code directory can only contain files of the same language. However, you may partition//分割开 the App_Code directory into subdirectories//子目录 (each containing files of the same language) in order to contain multiple languages under the App_Code directory. To do this, you need to register each subdirectory //子目录in the Web.config file for the application.
<configuration> <system.web> <compilation> <codeSubDirectories> <add directoryName="Subdirectory"/> </codeSubDirectories> </compilation> </system.web> </configuration>The following example demonstrates an App_Code directory partitioned //分割开to contain files in both the VB and C# languages.
C# Partitioned App_Code Folder Example
文件夹层次显示如下:

文件夹层次显示如下:
其中:CodeFolder2_cs.aspx
<%@ page language="C#" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
CustomClass c = new CustomClass();
Label1.Text = c.GetMessage(TextBox1.Text);
CustomClass2 c2 = new CustomClass2();
Label2.Text = c2.GetMessage(TextBox1.Text);
}
</script>
<html>
<head>
<title>ASP.NET Inline Pages</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Runat="server" />
<br />
<asp:Label ID="Label2" Runat="server" />
</form>
</body>
</html>
Web.config
<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="Subdirectory"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>
CustomClass.cs
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
public class CustomClass
{
public String GetMessage(String input) {
return "Hello " + input;
}
}
CustomClass.vb
'-----------------------------------------------------------------------
' This file is part of the Microsoft .NET SDK Code Samples.
'
' Copyright (C) Microsoft Corporation. All rights reserved.
'
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation. See these other
'materials for detailed information regarding Microsoft code samples.
'
'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'-----------------------------------------------------------------------
Imports Microsoft.VisualBasic
Public Class CustomClass2
Public Function GetMessage(ByVal name As String) As String
Return "Hello from VB " & name
End Function
End Class
The Bin Directory //二进制目录
Supported in ASP.NET version 1, the Bin directory is like the Code directory, except it can contain precompiled//预编译的 assemblies//组件. This is useful when you need to use code that is possibly written by someone other than yourself, where you don't have access to the source code (VB or C# file) but you have a compiled DLL//编译好的动态链接库 instead. Simply place the assembly//组件 in the Bin directory to make it available to your site. By default, all assemblies //组件in the Bin directory are automatically loaded in the app and made accessible to pages. You may need to Import //引入,进口specific namespaces//具体的命名空间 from assemblies //组件in the Bin directory using the @Import directive//指令 at the top of the page//在页面的头部.<@ Import Namespace="MyCustomNamespace" >
The Global Assembly Cache//全局组件缓存
The .NET Framework 2.0 includes a number of assemblies//组件 that represent //表现,描述the various parts of the Framework. These assemblies //组件are stored in the global assembly cache, which is a versioned//标明版本的 repository //贮藏室of assemblies//组件 made available to all applications on the machine (not just a specific application//对比前边的绿色部分说的 ,as is the case with Bin and App_Code //是不是可以译为"二进制目录和APP_Code目录才是这种情况<就是为某一个应用程序使用>"). Several assemblies//组件 in the Framework are automatically made available to ASP.NET applications. You can register additional assemblies//注册其他的组件 by registration in a Web.config file in your application.<configuration> <compilation> <assemblies> <add assembly="System.Data, Version=1.0.2411.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </assemblies> </compilation> </configuration>Note that you still need to use an @Import directive to make the namespaces in these assemblies available to individual pages.

浙公网安备 33010602011771号