Writing a Simple ASP .NET Page


Earlier, you learned how to build a simple Web application with Visual Studio .NET. You don't need to use VS .NET to build ASP .NET applications. An alternative is to use everyone's favorite Web application editor, notepad.exe (though any other text editor is fine as well).

To author ASP .NET applications without VS .NET:

  1. Create a directory in which to save your work.

  2. With Windows Explorer, locate the directory you created and right-click on it.

  3. Select Properties.

  4. Click the Web Sharing tab ( Figure 1.17 ).

    Figure 1.17. This feature in Windows Explorer makes it easy to create Web virtual directories. Each Web virtual directory represents a separate ASP .NET application.

    graphics/01fig17.gif

  5. Select the Share this folder option to view the Edit Alias dialog ( Figure 1.18 ).

    Figure 1.18. You don't have to name the virtual directory the same as your folder. The name you enter in this dialog is what goes after the .com part of the URL when a user is browsing. For example in www.microsoft.com/net , net is a virtual directory.

    graphics/01fig18.gif

  6. Press OK to accept the default settings.

  7. Click OK to commit changes.

  8. Left-click the directory you created in step 1.

  9. Right-click in the right-hand pane.

  10. Select New > Text Document.

  11. Enter default.aspx for the filename and press Enter. Click Yes when asked if you're sure you wish to rename the extension for the file.

  12. Right-click the newly created file and select Open With. Select notepad.exe from the list of programs.

  13. Enter the code in Figure 1.19 and save your changes.

    Figure 1.19 Unlike older versions of ASP, you can now use strongly typed variables. That means that you can declare variables as strings or integers, like in this example.
     <%@Page Language="c#" debug="true" %> <%@Import Namespace="System.Text" %> Thank you for purchasing C# VQS Guide:<br> <% StringBuilder str = new StringBuilder("ASP .NET is so "); for (int i = 0; i < 10 ;i++)    str.Append("cool "); str.Append("!"); Response.Write(str.ToString()); %> 
  14. From the Start menu, select Run and type http://localhost/nameofdir/default.aspx . Replace nameofdir with the name of the directory you created step 1 ( Figure 1.20 ).

    Figure 1.20. The text passed to the program through the Response.Write command appears in the browser.

    graphics/01fig20.gif

graphics/tick.gif Tips

  • If you don't see Web Sharing as a tab option (step 4), you may not have IIS installed in your operating system. See "Installing Internet Information Services (IIS)" earlier in this chapter.

  • The first step in creating ASP .NET applications is to create a virtual directory in IIS. A virtual directory defines a Web application. You created a virtual directory for IIS in steps 27. The virtual directory you created is not accessible to anonymous users (users that don't have permission to access your machine). To allow anyone to access the site you would need to change the permissions for your virtual directory through IIS.

  • It's very important that you name your pages with an .aspx extension. This extension signals IIS to hand the request to the ASP .NET ISAPI extension. An ISAPI extension is a DLL (Dynamic Link Library) that communicates with IIS to analyze client requests and provide client responses. You don't have to name your page default, but IIS automatically looks for a page named default if the client forgets the name of the document.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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