Professional ASP.NET 3.5 in C# and Visual Basic Part 134 docx

10 199 0
Professional ASP.NET 3.5 in C# and Visual Basic Part 134 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Evjen c27.tex V2 - 01/28/2008 3:51pm Page 1292 Chapter 27: Modules and Handlers using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //Logic to retrieve the image file context.Response.ContentType = "image/jpeg"; context.Response.WriteFile("Garden.jpg"); } public bool IsReusable { get { return false; } } } As you can see, you simply change the ContentType to image/jpeg to indicate that you are returning a JPEG image; then you use the WriteFile() method to write an image file to the output stream. Load the handler into a browser, and you see that the handler displays the image. Figure 27-8 shows the resulting Web page. Figure 27-8 1292 Evjen c27.tex V2 - 01/28/2008 3:51pm Page 1293 Chapter 27: Modules and Handlers Now, you create a simple Web page to display the image handler. Listing 27-9 shows code for the Web page. Listing 27-9: A sample Web page using the HttpHandler for the image source < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/ xhtml11.dtd" > < html xmlns="http://www.w3.org/1999/xhtml" > < head runat="server" > < title > HttpHandler Serving an Image < /title > < /head > < body > < form id="form1" runat="server" > < div > < img src="Handler.ashx" / > < /div > < /form > < /body > < /html > Although this sample is simple, you can enhance it by passing querystring parameters t o your han- dler and using them to perform additional logic in the handler. For instance, you can pass an ID in to dynamically retrieve an image from a SQL database and return it to the client, like this: < img src="Handler.ashx?imageid=123" / > Mapping a File Extension in IIS Although using the .ashx file extension is convenient, you might want to create an HTTP handler for a custom file extension or even for a commonly used extension. Use the code from the image handler to demonstrate this. Create a new class in the App_Code directory of your Web project. You can simply copy the code from the existing image handler control into this class, as shown in Listing 27-10. Notice that you removed the WebHandler directive because this is only a class and not a generic handler control. Other than that, the code is the same. Listing 27-10: The class-based image HttpHandler VB Imports System.Web Public Class MappedHandler : Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) _ Implements IHttpHandler.ProcessRequest context.Response.ContentType = "image/jpeg" context.Response.WriteFile("Garden.jpg") End Sub Public ReadOnly Property IsReusable() As Boolean _ Implements IHttpHandler.IsReusable Continued 1293 Evjen c27.tex V2 - 01/28/2008 3:51pm Page 1294 Chapter 27: Modules and Handlers Get Return False End Get End Property End Class C# using System.Web; public class MappedHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { //Logic to retrieve the image file context.Response.ContentType = "image/jpeg"; context.Response.WriteFile("Garden.jpg"); } public bool IsReusable { get { return false; } } } After your class is added, configure the application to show which file extension this handler serves. You do this by adding an httpHandlers section to web.config . Listing 27-11 shows the section to add for the image handler. Listing 27-11: Adding the HttpHandler configuration information to web.config < httpHandlers > < add verb="*" path="ImageHandler.img" type="MappedHandler, App_Code" / > < /httpHandlers > In the configuration section, you direct the application to use the MappedHandler class to process incom- ing requests for ImageHandler.img . You can also specify wildcards for the path. Specifying *.img for the path indicates that you want the application to use the MappedHandler class to process any request with the .img file extension. Specifying * indicates that you want all requests to the application to be processed using the handler. As with HttpModules, if you are running your Web application using IIS 7, then you will also need to add the HttpHandler configuration section to the < system.webServer > configuration section of your applications config file. When adding the handler configuration in this section, you also need to include the name attribute. < add name="ImageHandler" verb="*" path="ImageHandler.img" type="MappedHandler, App_Code" / > Load the ImageHandler.img file into a browser and, again, you should see that it serves up the image. Figure 27-9 shows the results. Notice the path in the browser’s address bar leads directly to the Image- Handler.img file. 1294 Evjen c27.tex V2 - 01/28/2008 3:51pm Page 1295 Chapter 27: Modules and Handlers Figure 27-9 Summary In this chapter, you learned a number of ways you can create modules which allow you to interact with the ASP.NET request processing pipeline. First you worked with HttpModules, which give you the power to plug yourself directly into the ASP.NET page-processing pipeline. The events provided to an HttpModule give you great power and flexibility to customize your applications. Finally, you looked at HttpHandlers. Handlers allow you to skip the ASP.NET page-processing pipeline completely and have 100 percent control over how the framework serves up requested data. You learned how to create your own image handler and then map the handler to any file or file extension you want. Using these features of ASP.NET can help you create features in your application which exercise great control over the standard page processing which ASP.NET uses. 1295 Evjen c28.tex V2 - 01/28/2008 3:52pm Page 1297 Using Business Objects One of the best practices in programming is to separate your application into workable and separate components — also known as business objects. This makes your applications far easier to manage and enables you to achieve the goal of code reuse because you can share these components among different parts of the same application or between entirely separate applications. Using business components enables you to build your ASP.NET applications using a true three-tier model where the business tier is in the middle between the presentation and data tiers. In addition, using business objects enables you to use multiple languages within your ASP.NET applications. Business objects can be developed in one programming language while the code used for the pre- sentation logic is developed in another. If you are moving any legacy applications or aspects of these applications to an ASP.NET environ- ment, you might find that you need to utilize various COM components. This chapter shows you how to use both .NET and COM components in your ASP.NET pages and code. This chapter also explains how you can mingle old ActiveX (COM) DLLs with new .NET com- ponents. So when all is said and done, you should feel somewhat relieved. You will see that you have not wasted all the effort you put into building componentized applications using the ‘‘latest’’ ActiveX technologies. Using Business Objects in ASP.NET 3.5 Chapter 1 of this book provides an introduction to using .NET business objects within your ASP.NET 3.5 applications. ASP.NET now includes a folder, \ App_Code , which you can place within your ASP.NET applications to hold all your .NET business objects. The nice thing about the App_Code folder is that you can simply place your uncompiled .NET objects (such as Calcula- tor.vb or Calculator.cs ) into this folder and ASP.NET takes care of compiling the objects into usable .NET business objects. Evjen c28.tex V2 - 01/28/2008 3:52pm Page 1298 Chapter 28: Using Business Objects Chapter 1 also shows how you can place within the App_Code folder multiple custom folders that enable you to use business objects written in different programming languages. Using this method enables ASP.NET to compile each business object into the appropriate DLLs to be used by your ASP.NET applications. Creating Precompiled .NET Business Objects Even though the App_Code folder is there for your use, you might choose instead to precompile your business objects into DLLs to be used by your ASP.NET applications. This is the method that was utilized prior to ASP.NET 2.0 and is still a method that is available today. You also might not have a choice if you are receiving your .NET business objects only as DLLs. First look at how to create a simple .NET business object using Visual Studio 2008. The first step is not to create an ASP.NET project but to choose File ➪ New ➪ Project from the Visual Studio menu. This launches the New Project dialog. From this dialog, select Class Library as the project type and name the project Calculator (see Figure 28-1). Figure 28-1 Using the Class1.vb or Class1.cs file that is created in the project for you, modify the class to be a simple calculator with Add , Subtract , Multiply ,and Divide functions. This is illustrated using Visual Basic in Figure 28-2. 1298 Evjen c28.tex V2 - 01/28/2008 3:52pm Page 1299 Chapter 28: Using Business Objects Figure 28-2 One point to pay attention to when you build your .NET components is the assembly’s metadata that is stored along with the assembly. Looking at the project’s properties, click the Application tab (the first tab available). Note that you can get to the project’s properties by right-clicking on the project title in the Solution Explorer. On this tab’s page, you will find a button labeled Assembly Information. Clicking this button gives you a dialog where you can put in the entire business object’s metadata, including the assembly’s versioning information (see Figure 28-3). You are now ready to compile the business object into a usable object. To accomplish this task, choose Build ➪ Build Calculator from the Visual Studio menu. This process compiles everything contained in this solution down to a Calculator.dll file. You will find this DLL in your project’s bin \ debug folder. By default, that will be C: \ Users \ [user] \ Documents \ Visual Studio 2008 \ Projects \ Calculator \ Calculator \ bin \ Debug \ Calculator.dll, only if you are using Windows Vista. Besides using Visual Studio 2008 to build and compile your business objects into DLLs, you can also accomplish this yourself manually. In Notepad, you simply create the same class file as was shown in Figure 28-2 and save the file as Calculator.vb or Calculator.cs depending on the language you are using. After saving the file, you need to compile the class into an assembly (a DLL). The .NET Framework provides you with a compiler for each of the targeted languages. This book focuses on the Visual Basic 2008 and C# 2008 compilers that come with the Framework. 1299 Evjen c28.tex V2 - 01/28/2008 3:52pm Page 1300 Chapter 28: Using Business Objects Figure 28-3 To compile this class, open the Visual Studio 2008 Command Prompt found at All Programs ➪ Microsoft Visual Studio 2008 ➪ Visual Studio Tools ➪ Visual Studio 2008 Command Prompt. From the provided DOS prompt, navigate to the directory that is holding your Calculator class (an example navigation command is cd c: \ My Files ). From the DOS prompt, type the following command if you are using the Visual Basic compiler: vbc /t: library Calculator.vb If your class is in C#, you use the following command: csc /t:library Calculator.cs As stated, each language uses its own compiler. Visual Basic uses the vbc.exe compiler found at C: \ Windows \ Microsoft.NET \ Framework \ v3.5 \. You will find the C# compiler, csc.exe , contained in the same folder. In the preceding examples, /t:library states that you are interested in compiling the Calculator.vb (or .cs ) class file into a DLL and not an executable ( .exe ), which is the default. Following the t:/library command is the name of the file to be compiled. There are many different commands you can give the compiler — even more than Visual Studio 2008 offers. For example, if you want to make references to specific DLLs in your assembly, you will have to 1300 Evjen c28.tex V2 - 01/28/2008 3:52pm Page 1301 Chapter 28: Using Business Objects add commands such as /r:system.data.dll . To get a full list of all the compiler options, check out the MSDN documentation. After you have run the commands through the compiler, the DLL is created and ready to go. Using Precompiled Business Objects in Your ASP.NET Applications To use any DLLs in your ASP.NET 3.5 project, you need to create a Bin folder in the root directory of your application by right-clicking on the project within the Solution Explorer and selecting Add ASP.NET Folder ➪ Bin. In Visual Studio 2008, the Bin directory’s icon appears as a gray folder with a gear next to it. Add your new DLL to this folder by right-clicking on the folder and selecting the Add Reference option from the menu provided. This launches the Add Reference dialog. From this dialog, select the Browse tab and browse until you find the Calculator.dll . When you find it, highlight the DLL and press OK to add it to the Bin folder of your project. This dialog is illustrated in Figure 28-4. Figure 28-4 Calculator.dll is added to your project and is now accessible by the entire project. This means that you now have access to all the functions exposed through this interface. Figure 28-5 shows an example of how IntelliSense makes exploring this .NET component easier than ever. As you can see, it is rather simple to create .NET components and use them in your ASP.NET applica- tions. Next, let’s look at using COM components. 1301 Evjen c28.tex V2 - 01/28/2008 3:52pm Page 1302 Chapter 28: Using Business Objects Figure 28-5 COM Interop: Using COM Within .NET Microsoft knows that every one of its legions of developers out there would be quite disappointed if they couldn’t use the thousands of COM controls that it has built, maintained, and improved over the years. Microsoft knows that nobody would get up and walk away from these controls to a purely .NET world. To this end, Microsoft has provided us with COM Interoperability. COM Interop (for short) is a technol- ogy that enables .NET to wrap the functionality of a COM object with the interface of a .NET component so that your .NET code can communicate with the COM object without having to use COM techniques and interfaces in your code. Figure 28-6 illustrates the Runtime Callable Wrapper, the middle component that directs traffic between the .NET code and the COM component. 1302 . put into building componentized applications using the ‘‘latest’’ ActiveX technologies. Using Business Objects in ASP. NET 3. 5 Chapter 1 of this book provides an introduction to using .NET business. within your ASP. NET 3. 5 applications. ASP. NET now includes a folder, App_Code , which you can place within your ASP. NET applications to hold all your .NET business objects. The nice thing about. the DLL is created and ready to go. Using Precompiled Business Objects in Your ASP. NET Applications To use any DLLs in your ASP. NET 3. 5 project, you need to create a Bin folder in the root directory

Ngày đăng: 05/07/2014, 19:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan