|
VB .NET/ASP .NET Asynchronous Web Service Program Example
In this exercise we will create two projects in the same solution. The first project is web service application (DLL) and the second project is VB .NET module (DLL). The project startup will be web page from the web service application.
Creating the ASP .NET/VB .NET code Web Service
Create a new ASP .NET/VB .NET web service application.
You can use the AuthServiceASPVB as the solution and project name if you want.
|
The ASP web service project template was given for us to customize it. Add/edit the following code.
Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. ' <System.Web.Script.Services.ScriptService()> _ <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ Public Class Service1 Inherits System.Web.Services.WebService 'a simple addition operation which return the sum <WebMethod()> _ Public Function Sum(ByVal lhs As Integer, ByVal rhs As Integer) As Integer Return lhs + rhs End Function
<WebMethod()> _ Public Function IsAuthorized(ByVal userName As String) As Boolean 'shun John and let everyone else in Dim r As New Regex("john", RegexOptions.IgnoreCase) Dim m As Match = r.Match(userName) 'if john If (m.Success) Then Return False Else ' others Return True End If End Function End Class |
Next, build the project and make sure there is no error.
Then run the project. Make sure there is no error.
The following is the web service test page run by the ASP .NET development server.
The ASP .NET development server short cut can be seen in the icon tray.
You can right-click the icon and we have three menus as shown below. Click the Show Details menu.
The following is the ASP .NET development server brief settings.
Next, let do some testing on the exposed methods. Click the IsAuthorized method, type John as the userName value and click the Invoke button.
The XML page will be displayed with the false value.
Next, test the Sum method. Key-in integer values and click the Invoke button.
The XML page shows the sum result.
Then, stop the ASP development server using the icon tray.
In the real development, to use the ASP web service, we need to create another HTML or asp page and you can try it by using the following code. Add new HTML page named index.
------------------------------------------------------------------
-------------------------------------------------------------------
Add/edit the code as shown below. It is just a simple form with a text box that receives an input.
<head> <title>The ASP web service test page</title> </head> <body> <form method="post" action='http://localhost:1707/Service1.asmx/IsAuthorized'> <input type="text" size="5" name="userName" style="width: 223px" /> <input type="submit" value="IsAuthorized?" /> </form> </body> </html> |
You can see the design by switching the editor to the design mode.
Next, test the page directly from the editor. Right-click mouse anywhere in the editor > Select View in Browser context menu.
Our HTML page with a simple form that receives an input looks fine. Type "John" and click the IsAuthorized button.
Well, all looks OK. Close the web and XML page.
Next let add the access denied page for the denied access user which is for John. In this case we are using asp page and name it as accessdenied.
Add/edit the following code.
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>The access denied page</title> </head> <body> <center><h1>Access Denied from AsyncModule</h1></center> </body> </html> |
Test the asp page directly from the editor.
The result is as intended. Close the page.
Then add the default page using asp which is for user other than John.
Add/edit the code as shown below.
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>The default asp page</title> </head> <body> <center> <% Response.Write("Hello, " & Server.HtmlEncode(User.Identity.Name)) %> </center> </body> </html> |
The following screenshot shows the code.
Test the page directly from the editor.
The page looks fine. Close the page.
We can set this page as the start page (though default.xxx page normally will be treated as the default web page by default).