Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/9 1:39:27
asp.net中Server对象的应用实例

asp.net中Server对象的应用实例

Server对象的应用

本节通过一个简单的实例来介绍如何使用Server对象来获得客户端和服务器的相关信息。实例运行结果如图1所示。



图1 Server对象的应用

程序开发步骤如下。

(1)新建一个网站,命名为15_05,其主页默认为Default.aspx。

(2)在Default.aspx页面中添加一个Table表格,用来布局页面,然后在该Table表格中添加5个Label控件,分别用来显示客户端和服务器端的相关信息。

(3)程序主要代码如下。

Default.aspx页面的Page_Load事件中,分别调用Server对象和Request对象的相关属性获取客户端和服务器端信息,并将这些信息显示在相应的Label控件中。Default.aspx页面的Page_Load事件代码如下:

protected void Page_Load(object sender, EventArgs e)

{

Label2.Text = Request.Browser.Platform;

Label3.Text = Request.Browser.Type;

Label5.Text = Server.MachineName.ToLower();

Label1.Text = Request.UserHostAddress;

string hostName = Dns.GetHostName();

IPAddress[] hostip;

hostip = Dns.GetHostAddresses(hostName);

foreach (IPAddress ip in hostip)

{

Label4.Text = ip.ToString();

}

}

完整程序代码如下:

★ ★★★★Default.aspx页面设计文件完整程序代码★★★★★

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>







无标题页















客户端/服务器端信息











&nbsp;&nbsp;

服务器端信息:







&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IP地址:


&nbsp; &nbsp; 计算机名称:





&nbsp;&nbsp;

客户端信息:







&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IP地址:


&nbsp; &nbsp; &nbsp; &nbsp; 操作平台:


&nbsp; &nbsp; 浏览器版本:



















★ ★★★★Default.aspx.cs页面代码文件完整程序代码★★★★★

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Net;

public partial class _Default : System.Web.UI.Page


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759