📜  VB.Net-Web编程

📅  最后修改于: 2020-11-19 09:06:38             🧑  作者: Mango


动态Web应用程序由以下两种程序之一或两者组成-

  • 服务器端脚本-这些是在Web服务器上执行的程序,使用ASP(活动服务器页面)或JSP(Java服务器页面)等服务器端脚本语言编写。

  • 客户端脚本-这些是在浏览器上执行的程序,使用JavaScript,VBScript等脚本语言编写。

ASP.Net是Microsoft引入的ASP的.Net版本,用于通过使用服务器端脚本来创建动态网页。 ASP.Net应用程序是使用.Net框架中存在的可扩展和可重用组件或对象编写的编译代码。这些代码可以使用.Net框架中类的整个层次结构。

可以使用以下两种语言编写ASP.Net应用程序代码-

  • Visual Basic .Net

  • C#

  • 脚本

  • J#

在本章中,我们将简要介绍如何使用VB.Net编写ASP.Net应用程序。有关详细讨论,请查阅ASP.Net教程。

ASP.Net内置对象

ASP.Net具有一些在Web服务器上运行的内置对象。这些对象具有在应用程序开发中使用的方法,属性和集合。

下表列出了ASP.Net内置对象,并带有简短描述-

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 among all users of an application. For example, you can use an Application object to create an e-commerce page.

2

Request

Describes the methods, properties, and collections of the object that stores information related to the HTTP request. This includes forms, cookies, server variables, and certificate data.

You use this object to access the information sent in a request from a browser to the server. For example, you can use a Request object to access information entered by a user in an HTML form.

3

Response

Describes the methods, properties, and collections of the object that stores information related to 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 output from your scripts to a browser.

4

Server

Describes the methods and properties of the object that provides methods for various server tasks. With these methods you can execute 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 utility functions on the server. For example, you may use the Server object to set a time out for a script.

5

Session

Describes the methods, properties, and collections of the object that 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 particular user sessions. For example, you can use Session object to keep information about the user and his preference and keep track of pending operations.

ASP.Net编程模型

ASP.Net提供两种类型的编程模型-

  • Web表单-这使您能够创建用户界面以及将应用于用户界面各个组件的应用程序逻辑。

  • WCF服务-这使您可以远程访问某些服务器端功能。

对于本章,您需要使用免费的Visual Studio Web Developer。 IDE几乎与用于创建Windows应用程序的IDE相同。

VS Web开发人员IDE

网络表格

Web表单包含-

  • 用户界面

  • 应用逻辑

用户界面由静态HTML或XML元素以及ASP.Net服务器控件组成。创建Web应用程序时,HTML或XML元素和服务器控件存储在扩展名为.aspx的文件中。此文件也称为页面文件。

应用程序逻辑由应用于页面中用户界面元素的代码组成。您可以使用任何.Net语言(例如VB.Net或C#)编写此代码。下图显示了“设计”视图中的Web窗体-

网络表格

让我们创建一个带有Web表单的新网站,当用户单击按钮时,该表单将显示当前日期和时间。采取以下步骤-

  • 选择文件→新建→网站。出现“新网站”对话框。

网络表单示例

  • 选择“ ASP.Net空网站”模板。键入网站的名称,然后选择保存文件的位置。

  • 您需要将默认页面添加到站点。在解决方案资源管理器中右键单击网站名称,然后从上下文菜单中选择“添加新项”选项。显示“添加新项”对话框-

网络表单示例

  • 选择“ Web表单”选项,并提供默认页面的名称。我们将其保留为Default.aspx。单击添加按钮。

  • 默认页面显示在源代码视图中

网络表单示例

  • 通过在Source视图中的页面的标记中添加一个值来设置Default网页的标题- </p> </li> <p><!-- <pre class="prettyprint notranslate"> <head runat="server"> <title>Tutorials Point -->

  • 要在网页上添加控件,请转到设计视图。在表单上添加三个标签,一个文本框和一个按钮。

网络表单示例
  • 双击按钮,并将以下代码添加到按钮的Click事件中-

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

使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码后,将在浏览器中打开以下页面-

网络表单示例

输入您的姓名,然后单击提交按钮-

网络表单示例

网页服务

Web服务是Web应用程序,它基本上是一类由其他应用程序可以使用的方法组成的类。尽管它没有用户界面,但它也遵循类似于ASP.Net网页的代码隐藏体系结构。

.Net Framework的早期版本使用ASP.Net Web Service的概念,该文件具有.asmx文件扩展名。但是,从.Net Framework 4.0起,Windows Communication Foundation(WCF)技术已经发展成为Web服务,.Net Remoting和其他一些相关技术的新继承者。它宁可将所有这些技术结合在一起。在下一节中,我们将简要介绍Windows Communication Foundation(WCF)。

如果使用的是.Net Framework的早期版本,则仍可以创建传统的Web服务。请查阅ASP.Net-Web服务教程以获取详细说明。

Windows Communication Foundation

Windows Communication Foundation或WCF提供了一个用于创建面向服务的分布式应用程序的API,称为WCF服务。

与Web服务一样,WCF服务也支持应用程序之间的通信。但是,与Web服务不同,此处的通信不仅限于HTTP。可以将WCF配置为通过HTTP,TCP,IPC和消息队列使用。支持WCF的另一个优点是,它支持双工通信,而使用Web服务,我们只能实现单工通信。

从初学者的角度来看,编写WCF服务与编写Web服务完全没有什么不同。为了简单起见,我们将看到如何-

  • 创建一个WCF服务

  • 创建服务合同并定义操作

  • 执行合同

  • 测试服务

  • 利用服务

为了理解这个概念,让我们创建一个简单的服务来提供股票价格信息。客户可以根据股票代码查询股票的名称和价格。为使此示例简单,将值硬编码在二维数组中。该服务将有两种方法-

  • GetPrice方法-将根据提供的代码返回股票价格。

  • GetName方法-根据提供的符号将返回股票名称。

创建WCF服务

采取以下步骤-

  • 为Web 2012打开VS Express

  • 选择“新建网站”以打开“新建网站”对话框。

  • 从模板列表中选择WCF服务模板-

创建WCF服务
  • 从“ Web位置”下拉列表中选择“文件系统”。

  • 提供WCF服务的名称和位置,然后单击“确定”。

  • 创建一个新的WCF服务。

创建服务合同并定义操作

服务合同定义了服务执行的操作。在WCF服务应用程序中,您将在解决方案资源管理器的App_Code文件夹中找到两个自动创建的文件。

  • IService.vb-这将具有服务合同;用简单的话来说,它将具有服务的接口以及服务将提供的方法的定义,您将在服务中实现这些方法。

  • Service.vb-这将实施服务合同。

WCF服务示例
  • 用给定的代码替换IService.vb文件的代码-

Public Interface IService
   
   Function GetPrice(ByVal symbol As String) As Double

   
   Function GetName(ByVal symbol As String) As String
End Interface

履行合同

在Service.vb文件中,您将找到一个名为Service的类,该类将实现IService接口中定义的服务合同。

将IService.vb的代码替换为以下代码-

' 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

测试服务

要运行如此创建的WCF服务,请从菜单栏中选择“调试”→“开始调试”选项。输出将是-

WCF服务测试

要测试服务操作,请从左窗格的树中双击操作的名称。新选项卡将出现在右窗格中。

在右窗格的“请求”区域中输入参数的值,然后单击“调用”按钮。

下图显示了测试GetPrice操作的结果-

WCF服务测试

下图显示了测试GetName操作的结果-

WCF服务测试

利用服务

让我们在将使用刚刚创建的WCF服务的相同解决方案中添加一个默认页面,即ASP.NET Web表单。

采取以下步骤-

  • 在解决方案资源管理器中右键单击解决方案名称,然后向解决方案添加新的Web表单。它将被命名为Default.aspx。

  • 在窗体上添加两个标签,一个文本框和一个按钮。

WCF服务利用
  • 我们需要向刚刚创建的WCF服务添加服务引用。在解决方案资源管理器中右键单击该网站,然后选择“添加服务参考”选项。这将打开“添加服务参考”对话框。

  • 在“地址”文本框中输入服务的URL(位置),然后单击“执行”按钮。它使用默认名称ServiceReference1创建一个服务引用。单击确定按钮。

添加服务参考

添加参考对您的项目有两项工作-

  • 在web.config文件中为服务创建地址和绑定。

  • 创建代理类以访问服务。

  • 双击表单中的“获取价格”按钮,在其Click事件上输入以下代码片段-

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
  • 使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码后,将在浏览器中打开以下页面-

WCF服务利用
  • 输入符号,然后单击“获取价格”按钮以获取硬编码的价格-

WCF服务利用