VB.Net – Web Programming
A dynamic web application consists of one or both of the following two types of programs −
-
Server-side scripting − these are programs running a Web servers written using server-side scripting languages such as ASP (Active Server Pages) or JSP (Java Server Pages).
-
Client-side scripting − These are programs that run in the browser and written with scripting languages like JavaScript, VBScript, etc.
ASP.Net is the .Net version of ASP introduced by Microsoft for building dynamic web pages using server-side scripts. ASP.Net applications are compiled code written using the extensible and reusable components or objects that exist in the .Net framework. These codes can use the entire class hierarchy in the .Net framework.
The ASP.Net application codes can be written in any of the following languages −
-
Visual Basic .Net
-
C#
-
Jscript
-
J#
In this chapter we give a very brief introduction to writing ASP.Net applications using VB.Net. For a detailed discussion, please consult the ASP.Net Tutorial.
Built-in objects in ASP.Net
ASP.Net has some built-in objects that run on a web server. These objects have methods, properties and collections that are used in application development.
The following table lists the objects built into ASP.Net with a brief description −
Sr.No. Object & Description 1
Application
Describes the methods, properties and collections of the object that stores information related to the entire web application, including variables and objects that exist for the lifetime of the application.
You use this object to store and retrieve information to be shared by all users of an application. For example, you can use an Application object to create an e-commerce site.
2
Request
Describes the methods, properties, and collections of the object which stores information related to the HTTP request. This includes forms, cookies, server variables, and certificate data.
You use this object to access the information that is sent from a browser to the server in a request. For example, you can use a Request object to access information entered into an HTML form by a user.
3
Response
Describes the methods, properties, and collections of the object that stores information regarding the server’s response. This includes displaying content, manipulating headers, setting locales, and redirecting requests.
You use this object to send information to the browser. For example, you use a Response object to send the output of your scripts to a browser.
4
Server
Describes the methods and properties of the object, which provides methods for various server tasks. You can use these methods to run code, get error conditions, encode text strings, create objects for use by the web page, and map physical paths.
You use this object to access various helper functions on the server. For example, you can use the Server object to set a script timeout.
5
Session
Describes the methods, properties, and collections of the object , which stores information related to the user’s session, including variables and objects that exist for the lifetime of the session.
You use this object to store and retrieve information about specific user sessions. For example, you can use the Session object to store information about the user and their preferences, and to keep track of pending operations.
ASP.Net Programming Model
ASP.Net offers two types of programming models −
-
Web Forms − this allows you to create the user interface and the application logic that is applied to various components of the user interface.
-
WCF Services – this allows you to remotely access some server-side functions.
See also: Create Your Translation Website, Kick Start Your Language Business
This chapter requires you to use the free Visual Studio Web Developer. The IDE is almost the same that you used to build the Windows applications.
Web Forms
Web Forms consist of −
-
User Interface
-
Application logic
The user interface consists of static HTML or XML elements and ASP.Net server controls. When you create a web application, HTML or XML elements and server controls are stored in a file with an .aspx extension. This file is also called a paging file.
Application logic consists of code that is applied to user interface elements on the page. You write this code in any .Net language like VB.Net or C#. The following image shows a web form in Design view −
Example
Let’s create a new website with a web form that displays the current date and time when a user clicks a button using the following steps −
-
Select File → New → Website The New Website dialog box appears.
-
Choose templates for blank ASP.Net websites Enter a name for the website and choose a location to save the files.
-
You must add a default page to the website, right-click the name of the site in the Solution Explorer and select Add New Item from the context menu The Add New Item dialog box appears −
-
Select the Web form option and provide a name for the default page, we have it as Default.aspx Click the Add button.
-
The default page is displayed in source view
-
Set the title for the default web page by typing in add a value to the page’s
tag in source view − -
To add controls to the web page, go to Design view and add three labels, a text box and Add a button.
-
Double-click the button and add the following code to the Sc button −
Protected Sub Button1_Click(sender As Object, e As EventArgs) _ Handles Button1.Click Label2.Visible = True Label2.Text = “Welcome to tutorials point: ” + TextBox1 .Text Label3.Text = “You visited us at: ” + DateTime.Now.ToString() End Sub
When the above code is run and run with the Start button available on the Microsoft Visual Studio toolbar , the following page opens in the browser −
Enter your name and click the Submit button −
Web Services
A web service is an Internet application, which is basically a class consisting of methods that other applications could use. It also follows a code-behind architecture like the ASP.Net web pages, although it doesn’t have a user interface.
The earlier versions of .Net Framework used this concept of ASP.Net Web Service, which had the File extension .asmx. However, starting with .Net Framework 4.0, Windows Communication Foundation (WCF) technology has emerged as a new successor to Web Services, .Net Remoting, and some other related technologies. Rather, it threw all of these technologies together. In the next section, we’ll give a brief introduction to Windows Communication Foundation (WCF).
If you’re using earlier versions of the .Net Framework, you can continue to build traditional web services. For a detailed description, see the ASP.Net – Web Services tutorial.
Windows Communication Foundation
Windows Communication Foundation or WCF provides an API for building distributed service-oriented applications, known as WCF services .
Like web services, WCF services enable communication between applications. Unlike web services, however, communication here is not just limited to HTTP. WCF can be configured to work over HTTP, TCP, IPC, and message queues. Another strong point in favor of WCF is that it supports duplex communication, whereas we could only achieve simplex communication with web services.
From a beginner’s perspective, writing a WCF service is not that different from to write a web service. To keep things simple, we’ll see how to −
-
create a WCF service
-
create a service contract and defines operation
-
Implementing the contract
-
Testing the service
-
Using the service
Example
To understand the concept, let’s create a simple service that provides stock price information. Clients can query a stock’s name and price based on the stock symbol. To keep this example simple, the values are hard-coded into a two-dimensional array. This service has two methods −
-
GetPrice method − it returns the price of a stock based on the provided symbol.
-
See also: How to Make Money on YouTube: 7 Effective Strategies
GetName method − returns the name of the stock based on the provided icon.
Creating a WCF Service
Follow the following steps −
-
Open VS Express for Web 2012
-
Select New Website to open the New Website dialog box.
-
Select WCF service template from list of templates −
-
Select File System from the Web Location drop-down list.
-
Enter a Name and location for the WCF service and click OK.
-
A new WCF service will be created.
Creating a service contract and defining the operations
A service contract defines the operation that a service performs. In the WCF service application you will find two files that are automatically created in the App_Code folder in the solution explorer
-
IService.vb − this contains the service contract, more simply the interface for the service, with the definitions of the methods that the service exposes and that you implement in your service.
p>
-
Service.vb − This implements the service contract.
-
Replace the Code of the IService.vb file by the specified code −
Public Interface IService Function GetPrice(ByVal symbol As String) As Double Function GetName(ByVal symbol As String) As String End Interface
Implementation of the contract
In the Service.vb file you will find a class called Service that uses the IService defined service contract.
Replace the code of IService.vb with the following code −
‘ NOTE: You can use the “Rename” command in the context menu to change the class name ” Service” in code, SVC and configuration file together. Public Class Service implements IService Public Sub New() End Sub Dim stocks As String(,) = {{“RELIND”, “Reliance Industries”, “1060.15”}, {“ICICI”, “ICICI Bank”, “911.55”} , {“JSW”, “JSW Steel”, “1201.25”}, {“WIPRO”, “Wipro Limited”, “1194.65”}, {“SATYAM”, “Satyam Computers”, “91.10”} } Public function GetPrice( ByVal symbol As String) As Double _ Implements IService.GetPrice Dim i As Integer ‘it takes the symbol as a parameter and returns the price For i = 0 To i = stocks.GetLength(0) – 1 If (String.Compare(symbol , stocks(i, 0)) = 0) Then Return Convert.ToDouble(stocks(i, 2)) End If Next i Return 0 End Function Public function GetName(ByVal symbol As String) As String _ Implements IService.GetName ‘ Es takes the symbol as a parameter and ‘ returns the name of the stock Dim i As Integer For i = 0 To i = stocks.GetLength(0) – 1 If (String.Compare(symbol, stocks(i, 0)) = 0) Then Return stocks (i, 1) Exit if next i return stock not found. end of function. End of class For the WCF service created in this way, select Debug → Start Debugging from the menu bar. The output would be −
Double click to test In the service operations tree structure in the left pane, click the name of the operation. A new tab will appear on the right side.
Enter the value of the parameters in the request area on the right side and click the “Invoke” button.
The following diagram shows the result of testing the operation GetPrice −
The following diagram shows the result of testing the operation GetName −
Using the service
Let’s add a standard page, an ASP.NET web form, in the same solution from which we are going to use the WCF service we just created.
Perform the following steps −
-
Right click on the solution name in the solution tree Explorer and add a new web form to the solution. It will be named Default.aspx.
-
Add two labels, a text box and a button to the form.
-
We need to add a service reference to the WCF service we just created. Right-click the website in Solution Explorer and select Add Service Reference. This will open the Add Service Reference dialog.
-
Enter the URL (location) of the service in the Address text box and click the Go button . It creates a service reference with the default name ServiceReference1. Click the OK button.
Adding the reference does two jobs for your project −
-
Generates the address and binding for the service in the web.config.
file
-
Creates a proxy class to access the service.
-
Double-click the “Get Price” button on the form to get the enter the following code snippet on his click event −
Subclass _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(Sender As Object, e As EventArgs) _ Handles Button1.Click Dim ser As ServiceReference1.ServiceClient = _ New ServiceReference1.ServiceClient Label2.Text = ser.GetPrice(TextBox1.Text).ToString() End Sub End Class
-
When the above code is executed and run from the Start button available on the Microsoft Visual Studio toolbar, the following page opens in the browser −
-
Enter an icon and click Get Price button to get the hardcoded price −
See also: What Secondhand Fashion Brands Must Learn About Developing A Marketplace App Like Poshmark
.