May 13, 2021 vb.net
Dynamic Web applications include one or both of the following types of programs:
Server-side scripting - These are programs that are executed on a Web server and are written in server-side scripting languages such as Active Server Pages or Java Server Pages.
Client Scripts - These are programs that are executed on a browser and written in scripting languages such as JavaScript, VBScript, and so on.
Asp. N et is the .Net version of ASP introduced by Microsoft to create dynamic web pages by using server-side scripts. A sp. N et applications are compiled code written using extensable and re-usable components or objects that exist in the .Net framework. T hese codes can be used. T he entire hierarchy of classes in the Net framework.
Asp. N et application code can be written in any of the following languages:
Visual Basic .NET
C#
Jscript script
J#
In this chapter, we'll briefly describe VB.Net using ASP.Net applications. F or more detailed discussion, see ASP. N et tutorial.
Asp. N et has some built-in objects running on the Web server. These objects have methods, properties, and collections that are used in application development.
The following table lists the built-in objects ASP.Net brief description:
Objective | Describe |
---|---|
Application
Application
|
Describes the methods, properties, and collections of objects that store information about the entire Web application, including variables and objects that exist throughout the application life cycle.
|
Request Request |
Describes the methods, properties, and collections of objects that store information related to HTTP requests.
This includes forms, cookies, server variables, and certificate data.
|
Response
Response |
Describes the methods, properties, and collections of objects that store information about the server response.
This includes displaying content, operating headers, setting locales, and redirecting requests.
|
Server
Server |
Describes the methods and properties of objects that provide methods for various server tasks.
Using these methods, you can execute code, get error conditions, encode text strings, create objects for use by Web pages, and map physical paths.
|
Session
Session |
Describes the methods, properties, and collections of objects that store information related to a user session, including variables and objects that exist during the lifetime of the session.
|
Asp. N et provides two types of programming models:
Web Forms - This enables you to create user interfaces and application logic that will be applied to the various components of the user interface.
WCF Services - This gives you remote access to some server-side features.
For this chapter, you'll need to use the free Visual Studio Web Developer. T he IDE is almost identical to the one you already use to create Windows applications.
Web forms include:
The user interface
Application logic
The user interface includes static HTML or XML elements and ASP.Net server controls. W hen you create a Web application, HTML or XML elements and server controls are stored in .aspx with an extension. This file is also known as a page file.
Application logic includes code that applies to user interface elements in a page. Y ou can use any. Net languages, such VB.Net write code in your system or C.
The following image shows the Web form in the Design view:
Let's create a new Web site with a Web form that shows the current date and time when the user clicks the button. F ollow these steps:
Select a file - New - Web site. T he New Site dialog box appears.
Select ASP.Net site template. T ype the name of the Web site, and then choose where to save the file.
You need to add a default page to the site. R ight-click the name of the site in Solution Explorer, and then select the option to add a new project from the context menu. T he "Add a new item" dialog box will be displayed:
Select the Web form option and provide the name of the default page. W e save it as a Default .aspx. C lick the Add button.
The default page appears in the source view
Set the title of the Default page by adding a value to Values
To add controls to a Web page, go to Design View. A dd three labels, a text box, and a button to the form.
Double-click the button and add the following code to the click event for the 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 you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following pages open in your browser:
Enter your name and click the submit button:
A Web service is a Web application that is essentially a class of methods that other applications can use. It also follows a code-behind architecture, such as ASP.Net, although it does not have a user interface.
An earlier version of the .Net Framework ASP.Net the concept of Web Service, which has a .asmx file extension. H owever, starting with .Net Framework 4.0, Windows Communication Foundation (WCF) technology has developed into a new successor to Web Services, .NetRemoting, and a number of other related technologies. I t combines all these technologies. In the next section, we'll briefly describe the Windows Communication Foundation (WCF).
If you use a previous version of the .Net Framework, you can still create traditional Web services. F or detailed instructions, see ASP.Net - Web Services for detailed instructions.
Windows Communication Foundation or WCF provides an API for creating distributed service-oriented applications called WCF services.
Like Web services, WCF services support communication between applications. H owever, unlike Web services, communication here is not limited to HTTP. W CF can be configured for use through HTTP, TCP, IPC, and message queues. Another strong point in supporting WCF is that it provides support for duplecom communications, where we can only implement single-worker communication for Web services.
From a beginner's point of view, writing WCF services is not completely different from writing Web services. To keep it simple, we'll see how:
Create a WCF service
Create a service contract and define the action
Execute the contract
Test the service
Use the service
To understand this concept, let's create a simple service that provides stock price information. C ustomers can query the name and price of a stock based on the stock code. T o keep this example simple, these values are hard-coded in a two-dimensional array. T here are two ways to do this service:
GetPrice method - It will return the price of the stock, based on the symbol provided.
GetName method - It will return the name of the stock, based on the symbol provided.
Create a WCF service
Follow these steps:
Open VS Express for Web 2012
Select a new site and open the New Site dialog box.
Select the WCF service template in the template list:
Select the file system from the Web location drop-down list.
Provide the name and location of the WCF service, and then click OK.
Create a new WCF service.
Create a service contract and define actions
A service contract defines what the service does. In the WCF service application, you'll find that two files are automatically App_Code in the library folder in Solution Explorer
IService .vb - this will have a service contract; I n simple terms, it will have a service interface, with a definition of the method that the service will provide that you will implement in your service.
Service .vb - This will implement a service contract.
Replace the code for the IService .vb file with the given code:
Public Interface IService <OperationContract()> Function GetPrice(ByVal symbol As String) As Double <OperationContract()> Function GetName(ByVal symbol As String) As String End Interface
Implement the contract
In the service .vb file, you'll find a class called Service that implements the service contract defined in the IService interface.
Replace the code for the IService .vb the following code:
' NOTE: You can use the "Rename" command on the context menu to change the class name "Service" in code, svc and config 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 parameter and returns 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 ' It takes the symbol as parameter and ' returns 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) End If Next i Return "Stock Not Found" End Function End Class
Test the service
To run the WCF service created as such, select the Debug-Start Debugging option from the menu bar. T he output will be:
To test a service operation, double-click the name of the operation from the tree in the left pane. The new tab appears in the right pane.
Enter the parameter value in the Request area of the right pane, and then click the Call button.
The following image shows the results of testing getPrice operations:
The following image shows the results of testing getName operations:
Use the service
Let's add a default page, a web form, to the ASP.NET solution, and we'll use the WCF service we just created.
Follow these steps:
Right-click the solution name in Solution Explorer and add a new Web form to the solution. I t 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. R ight-click the web site in Solution Explorer, and then select the Option to add a service reference. This opens the Add Service Reference dialog box.
Enter the URL (location) of the service in the address text box, and then click the execute button. I t uses the default name ServiceReference1 to create a service reference. C lick the OK button.
Adding a reference does two jobs for your project:
Partial Class _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 you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following pages open in your browser:
Enter the symbol and click the Get Price button to get a hard-coded price: