By default, ASP.NET makes a small set of commonly used .NET assemblies available to all web pages.
These assemblies (listed in Table 2-4) are configured through a special machine-wide configuration file.
You don’t need to take any extra steps to use the classes in these assemblies.
If you want to use additional features or a third-party component, you may need to import more
assemblies. For example, if you want to use an Oracle database, you need to add a reference to the
System.Data.OracleClient.dll assembly. To add a reference, select Website ➤ Add Reference (or
Project ➤ Add Reference in a web project). The Add Reference dialog box will appear, with a list of
registered .NET assemblies (see Figure 2-14).
■ Note Visual Studio 2010 has enhanced the Add Reference window to use asynchronous loading. As a result, it
appears much quicker and doesn’t freeze you out while it scans your system for assemblies. However, while these
assemblies are being added to the list, you may find it difficult to select the item you want before it “jumps” to a
new position.
In the Add Reference dialog box, select the component you want to use. If you want to use a
component that isn’t listed here, you’ll need to click the Browse tab and select the DLL file from the
appropriate directory (or from another project in the same solution, using the Projects tab).
If you’re working with a projectless website and you add a reference to another assembly, Visual
Studio modifies the web.config file to indicate the assembly you’re using. Here’s an example of what you
might see after you add a reference to the System.Web.Routing.dll file:
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly=
"System.Web.Routing, Version=4.0.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
If you’re working with a web project, and you add a reference to another assembly, Visual Studio
doesn’t need to change the web.config file. That’s because Visual Studio is responsible for compiling the
code in a web project, not ASP.NET. Instead, Visual Studio makes a note of this reference in the .csproj
project file. The reference also appears in the Solution Explorer window under the References node. You
can review your references here, and remove any one by right-clicking it and choosing Remove.
If you add a reference to an assembly that isn’t stored in the GAC (global assembly cache), Visual
Studio will create a Bin subdirectory in your web application and copy the DLL into that directory. (This
happens regardless of whether you’re using project-based or projectless development.) This step isn’t
required for assemblies in the GAC because they are shared with all the .NET applications on the
computer.
If you look at the code for a web-page class, you’ll notice that Visual Studio imports just a few core
.NET namespaces. Here’s the code you’ll see:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
Adding a reference isn’t the same as importing the namespace with the using statement. The using
statement allows you to use the classes in a namespace without typing the long, fully qualified class
names. However, if you’re missing a reference, it doesn’t matter what using statements you include—the
classes won’t be available. For example, if you import the System.Web.UI namespace, you can write
Page instead of System.Web.UI.Page in your code. But if you haven’t added a reference to the
System.Web.dll assembly that contains these classes, you still won’t be able to access the classes in the
System.Web.UI namespace.
Hiç yorum yok:
Yorum Gönder