0% found this document useful (0 votes)
10 views3 pages

Code Sharing Model in AVS

This Document is for ASP.NET or .NET Framwork.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Code Sharing Model in AVS

This Document is for ASP.NET or .NET Framwork.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Code Sharing In ASP.

NET

Code sharing is a mechanism with which common code is shared among different pages of
your web application. It is important or beneficial when you want to make the code, with
specific functionality, accessible to all the web application. There are three methods are used
for code sharing model.

1) Using the code directory


2) Using the bin directory
3) Using the Global Assembly Cache

Using the code directory:

ASP.NET provides a unique directory known as the App_Code to store the code files that are
accessible to all the web pages of an application. When you add something in App_Code
folder, such as class or .wsdl files, it is automatically detected and compiled. Once compiled,
any page in your application can access them.

You can store source code in the App_Code folder, and it will be automatically compiled at run
time. The resulting assembly is accessible to any other code in the Web application. The
App_Code folder therefore works much like the Bin folder, except that you can store source
code in it instead of compiled code. The App_Code folder and its special status in an ASP.NET
Web application makes it possible to create custom classes and other source-code-only files
and use them in your Web application without having to compile them independently.

The App_Code folder can contain source code files written as traditional class files — that is,
files with a .vb extension, .cs extension, and so on. However, it can also include files that are
not explicitly in a specific programming language. Examples include .wsdl (Web service
description language) files and XML schema (.xsd) files. ASP.NET can compile these files into
assemblies.

Bin Directory

You can store compiled assemblies in the Bin folder, and other code anywhere in the Web
application (such as code for pages) automatically references it. A typical example is that you
have the compiled code for a custom class. You can copy the compiled assembly to the Bin
folder of your Web application and the class is then available to all pages.

Assemblies in the Bin folder do not need to be registered. The presence of a .dll file in the Bin
folder is sufficient for ASP.NET to recognize it. If you change the .dll and write a new version
of it to the Bin folder, ASP.NET detects the update and uses the new version of the .dll for new
page requests from then on.

Copyright by Dr Ashoksinh Solanki(CASPS) 2024-25 Subject:505-ASP.NET Page 1


Example of Code directory:

Right click the URL in the Solution Explorer and add App-Code directory

‘sample code in App_Code

Imports Microsoft.VisualBasic

Public Class Class1

' write function sum to add two numbers

Public Function sum(ByVal a As Integer, ByVal b As Integer) As Integer

Return a + b

End Function

End Class

Now you can access this code in your entire web application

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles Button1.Click

' access common code from App_Code folder using sum() function

' create an object for class

Dim o1 As New Class1

'call method sum()

Label3.Text = o1.sum(TextBox1.Text, TextBox2.Text)

End Sub

End Class

Copyright by Dr Ashoksinh Solanki(CASPS) 2024-25 Subject:505-ASP.NET Page 2


Global Assembly Cache (GAC)

Each computer on which the Common Language Runtime is installed has a machine-wide code
cache called the 'Global Assembly Cache'. GAC is a folder in Windows directory to store the
.NET assemblies that are specifically designated to be shared by all applications executed on a
system. Assemblies can be shared among multiple applications on the machine by registering
them in global Assembly cache(GAC).

The GAC is automatically installed with the .NET runtime. The global assembly cache is located
in 'Windows/WinNT' directory and inherits the directory's access control list that administrators
have used to protect the folder.

Copyright by Dr Ashoksinh Solanki(CASPS) 2024-25 Subject:505-ASP.NET Page 3

You might also like