Fun Pet Trick 4: The Code Directory (and its Cross-Language Usage)


Fun Pet Trick #4: The Code Directory (and its Cross-Language Usage)

By Scott Guthrie

One of the fun improvements made in ASP.NET 2.0 is the introduction of the code directory. This directory lives immediately underneath the application root of an ASP.NET application, and enables you to easily add non-UI code, resources, and web services to your application without requiring developers to manually compile a DLL to do so.

Note that you can use this feature within any development tool (Notepad, Emacs, Dreamweaver, FrontPage 11, and so on). It really shines, though, with VS .NET—which has native IntelliSense support for it.

Here are some steps to see this in action:

  1. Launch Visual Studio and create or open a blank web page.

  2. Within the Solution Explorer, create a new code directory.

  3. Within the code directory, right-click and add a new class. Select C# as the language.

  4. Add an Add() method to the class in the file you just created:

     using System; public class Class1 {     public int Add (int x, int y)     {         return x + y;      } } 

  5. Click the Save button.

  6. Now create a new C# page within the application (example: page1.aspx).

  7. Add a button to the design surface, and then double-click it to generate the event handler.

  8. Within the event handler, you can now instantiate and call the class you just created. Notice that you get full IntelliSense support for it.

  9. Call the method and update the button's text:

     void Button1_Click(object sender, System.EventArgs e) {     Class1 test = new Class1();     Button1.Text = "Add Method: " + test.Add (6, 5); } 

  10. Press Ctrl+F5 and notice that the code works—no compilation is required.

  11. Keeping the browser open, try changing the values passed to the Add() method to 7 and 5. Click Save in the ASPX file, then click the button again in the browser. Notice that the change is immediately updated—no explicit recompile is required.

  12. Now create a new page within your web site—this time create the page using VB .NET.

  13. Drag and drop a button onto the design surface, and then double-click the button to add an event handler.

  14. Now try using the C# class you previously created. Notice that you get full IntelliSense support, despite the fact that you are using two different languages within the same project (something VS has never done before).

  15. Call the Add() method and update the Button control:

     Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)     Dim test As New Class1     Button1.Text = "Add Method: " & test.Add(5, 6) End Sub 

  16. Run the page (press Ctrl+F5) and notice that everything works.

  17. Now set a breakpoint on the Dim statement just shown (press F9 on the line), and then debug the page (press F5). Note that you might need to click Yes to add a web.config file with debugging enabled if you don't have that already set up.

  18. Once the page comes up, click the button. The debugger will now break on the first line of the event handler. Press F10 to step over it to the second line.

  19. Then press F11 to step into the Add call. Notice that you can seamlessly debug from C# to VB within the same project.

  20. Hover the cursor over the x and y variables to see their values show up in the debugger (without having to look at the watch/local window). Press F5 when you are done to complete the page request.

The two big benefits of the code directory are that it enables much quicker development (no long compiles required—just click Save to update things), and you can now move to cross-language development within your web sites (which can be awfully nice). In a shared project scenario (using source control), you also now no longer need to check out and check in the project file when adding a component (which leads to contention or collisions when multiple people do it)—just add it to the code directory and away you go.




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net