Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Wednesday, February 01, 2006

Creating Your First ASP.NET Application(Chapter 1)

For your first sample ASP.NET projects, let’s take a look at some very simple examples, first using VB.NET, and then, for comparison, the same project built with C#. As you will see,ASP.NET is very easy, and you will be up and running in no time at all.
1. Start a new document in either Visual Studio.NET or the text editor of your choice.
2. Enter the code from Figure 1.3 into the document, and then go to File Save As and name it HelloWorld.ASPX in your Web root folder.
Figure 1.3 Hello World Example
<html>
<head>
<title>Example 1: Hello World</title>
</head>
<body bgcolor=white>
<h1>
<% response.write("Hello World") %>
</h1>
</body>
</html>
3. Launch your Web browser and enter the location of the new file (e.g., localhost/helloworld.aspx).You should see something like the screenshot in Figure 1.4. This HTML markup should all be familiar; it is just a basic Web page.The main difference you will notice is the addition of code within <% and %> tags. This is our ASP.NET code. By default,ASP.NET uses VB.NET language (we will look at C# later).
<% response.write("Hello World") %>
Figure 1.4 Script from Figure 1.3 Displayed in a Browser


This code tells the server to output the text “Hello World” to the user’s browser. Alternative shorthand for outputting values is to use the following form, where value is the variable or literal you wish to output.
<%=value%>
Since that is not much of an example, and nothing you couldn’t do as well in classic ASP, or HTML for that matter, let’s expand the example a bit.With the code in Figure 1.5, we will use the ASP.NET browser capability function of the Request object.
Figure 1.5 Hello World with Browser Capabilities Example
<html>
<head>
<title>Example 1: Hello World</title>
</head>
<body bgcolor=white>
<%
dim strUsersBrowser as string
strUsersBrowser&=request.browser.browser
strUsersBrowser&=cstr(request.browser.majorversion)
strUsersBrowser&="."
strUsersBrowser&=cstr(request.browser.minorversion)
response.write("<h1>Your web browser is " & strUsersBrowser & "</h1>")
%>
</body>
</html>
Within this code, you can see that we first declare we want to use a new string variable, which we will use to store and display the user’s browser type:
dim strUsersBrowser as string
Next, we add the result of the Request.Browser.Browser object property to our string.This method returns the name of the visitor’s browser: strUsersBrowser+=request.browser.browser
Then, we use the .majorversion and .minorversion properties converted to strings using CStr, which will return the version numbers of the browser: strUsersBrowser+=cstr(request.browser.majorversion)
Finally, we output the result to the user with Response.Write. In Classic ASP we would have had to create a reference to a browser capabilities component and ensured that our browsecap.ini configuration file was up to date.With the new in-built browser capabilities feature, we simply have to request the values, and in theory at least the browser name and version should always be up to date as the browser version is detected by using regular expressions. Figures 1.6 and 1.7 show the script display in IE6 and Netscape 6.
As explained earlier,Microsoft has introduced a new language especially for .NET, called C#. As this is now Microsoft’s flagship language, and the most likely language to be supported by Open Source projects, it is probably useful to show you now how our previous example looks when coded in C#.
Figure 1.6 Browser Detect with IE6

Figure 1.7 Browser Detect with Netscape 6.0

Figure 1.8 takes the browser detection example and simply recodes it into the C# syntax.The very first line shows the first distinction between this and the VB.NET version.VB.NET is the default language of ASP.NET, and, therefore, to use that language you just start coding. On the other hand, if you want to use C#, you must declare this with the language declaration. Another major difference is that C# is case sensitive. If you had entered request rather than Request, the compiler will return with “The type or namespace name ‘request’ could not be found.”This is a common source of errors for VBScript programmers learning C#; as in VBScript, case is largely a matter of personal programming style.
The third difference is how lines of code are terminated. In C#, lines end with a semicolon, while in VBScript and VB.NET the lines end with a carriage return. Comments in C# take the form of two forward slashes (“//”). In VB.NET and VBScript it was an apostrophe.This form of comment must not flow over more than one line. If you require multi-line comments, then either enter double slashes at the beginning of each line or use the alternative form of “/*” at the beginning of the comment and “*/” at the end.
Remaining differences are the variable declaration where we use “string variablename” rather than “dim variablename” and we use “.ToString()” instead of “CStr,” and strings are concatenated with a plus symbol instead of the ampersand in VB.C# will of course be familiar to C programmers, but also should be quite familiar to anyone who has programmed in Java, JavaScript, and so on. It is a nice, fresh, clean language, with all of the best bits of C++ and Visual Basic without the clumsy baggage. Even though VB.NET will be many programmers’ bread and butter language, C# is well worth the effort to learn.
Figure 1.8 Example C# Code
<%@ page language="c#" %>
<html>
<head>
<title>Chapter 1</title>
</head>
<body bgcolor=white>
<%
/*
comments are either entered with slashes like below
or multi-line comments can be entered like this
*/
// # we declare string variables with string rather than dim
string strUsersBrowser = "";
// # make sure you use the correct case!
strUsersBrowser+=Request.Browser.Browser;
strUsersBrowser+=Request.Browser.MajorVersion.ToString();
strUsersBrowser+=".";
strUsersBrowser+=Request.Browser.MinorVersion.ToString();
// # strings are concatenated with + in C#
Response.Write("<h1>Your web browser is " + strUsersBrowser + "</h1>");
%>
</body>
</html>

0 Comments:

Post a Comment

<< Home