Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > PHP > web程序中获取应用程序系统变量的方法( For.net 1.1)
【标  题】:web程序中获取应用程序系统变量的方法( For.net 1.1)
【关键字】:web,For.net,1.1
【来  源】:http://blog.csdn.net/downmoon/archive/2006/12/03/1428110.aspx

web程序中获取应用程序系统变量的方法( For.net 1.1)

Your Ad Here 获取系统变量在.net 的web应用程序中的确是非常方便,比如获取 "http://ap2:8080/"或者"http://10.0.0.1/mypplication/"等诸如此类的系统变量。
注:本文主要是针对.net Framework 1.0、1.1的情况,因为.net 2.0的命名空间已经发生了很大的变化,在后面的文章中将会专门加以介绍

为描述方便,先新建一aspx文件,前台文件如下:

<body MS_POSITIONING="GridLayout">
        
<form id="Form2" method="post" runat="server">
            
<FONT face="宋体">
                
<asp:Button id="btn_Page" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px" runat="server"
                    Text
="通过Page获取"></asp:Button>
                
<asp:Label id="lbInfo1" style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server"></asp:Label>
                
<asp:Button id="btn_ServerVal" style="Z-INDEX: 103; LEFT: 520px; POSITION: absolute; TOP: 16px"
                    runat
="server" Text="通过ServerVariables获取"></asp:Button>
                
<asp:Label id="lbInfo2" style="Z-INDEX: 104; LEFT: 48px; POSITION: absolute; TOP: 80px" runat="server"></asp:Label>
                
<asp:Label id="lbInfo3" style="Z-INDEX: 105; LEFT: 48px; POSITION: absolute; TOP: 104px" runat="server"></asp:Label>
                
<asp:Button id="btn_Cintext" style="Z-INDEX: 106; LEFT: 280px; POSITION: absolute; TOP: 16px"
                    runat
="server" Text="通过Context获取"></asp:Button></FONT>
        
</form>
    
</body>



第一种方法,直接在在页面中获取。利用Page.Request命名空间。

private void btn_Page_Click(object sender, System.EventArgs e)
        
{
            
this.lbInfo2.Text="";
            
this.lbInfo3.Text="";
            
this.lbInfo1.Text=Page.Request.ApplicationPath+"<br/>"
                
+Page.Request.CurrentExecutionFilePath+"<br/>"
                
+Page.Request.UserHostAddress+"<br/>"
                
+Page.Request.Url.Host+"<br/>"
                
+Page.Request.RawUrl+"<br/>"
                
+Page.Request.UserHostName+"<br/>"
                
+Page.Request.Url.HostNameType+"<br/>"
                
+Page.Request.Url.Port+"<br/>"
                
+Page.Request.UrlReferrer.Host+"<br/>"
                
+Page.Request.Url.LocalPath+"<br/>"
                
+Page.Request.Url.Authority+"<br/>"
                
+Page.Request.Url.UserInfo+"<br/>"
                
+"<br/>";
        
        }


第二种方法:在非Web项目中用Context的Request方法。

private void btn_Cintext_Click(object sender, System.EventArgs e)
        
{
            
this.lbInfo1.Text="";
            
this.lbInfo3.Text="";
            
this.lbInfo2.Text=
                System.Web.HttpContext.Current.Server.MachineName
+"<br/>"
                
+System.Web.HttpContext.Current.Request.ApplicationPath+"<br/>"
                
+System.Web.HttpContext.Current.Request.CurrentExecutionFilePath+"<br/>"
                
+System.Web.HttpContext.Current.Request.RawUrl+"<br/>"
                
+System.Web.HttpContext.Current.Request.Url+"<br/>"
                
+System.Web.HttpContext.Current.Request.UserHostAddress+"<br/>"
                
+System.Web.HttpContext.Current.Request.UserHostName+"<br/>"
                
+System.Web.HttpContext.Current.Request.RequestType+"<br/>"
                
+System.Web.HttpContext.Current.Request.UrlReferrer.Host+"<br/>"
                
+System.Web.HttpContext.Current.Request.UrlReferrer.HostNameType+"<br/>"
                
+System.Web.HttpContext.Current.Request.Path+"<br/>"
                
+System.Web.HttpContext.Current.Request.Url.Port+"<br/>"
                
+System.Web.HttpContext.Current.Request.Url.IsDefaultPort.ToString()+"<br/>"
                
+System.Web.HttpContext.Current.Request.Url.IsFile.ToString()+"<br/>"
                
+System.Web.HttpContext.Current.Request.Url.LocalPath+"<br/>"
                
+"<br/>";
        
        }


第三种方法:可以从Request.ServerVariables的集合中获取 。获取的变量也最全。

private void btn_ServerVal_Click(object sender, System.EventArgs e)
        
{
        
            
this.lbInfo1.Text="";
            
this.lbInfo2.Text="";
            
int loop1, loop2;
            System.Collections.Specialized.NameValueCollection coll;

            
//以下两种方式均可
            
//coll=Page.Request.ServerVariables; 
            coll=System.Web.HttpContext.Current.Request.ServerVariables; 
            
this.lbInfo3.Text="";
            
// Get names of all keys into a string array. 
            String[] arr1 = coll.AllKeys; 
            
for (loop1 = 0; loop1 < arr1.Length; loop1++
            
{
                
this.lbInfo3.Text+=("Key: " + arr1[loop1] + "<br>");
                String[] arr2
=coll.GetValues(arr1[loop1]);
                
for (loop2 = 0; loop2 < arr2.Length; loop2++
                
{
                    
this.lbInfo3.Text+=("Value " + loop2 + "" + Server.HtmlEncode(arr2[loop2]) + "<br>");
                }

            }

        }






Http协议中的方法:【上一篇】
PHP实现的简单日历代码:【下一篇】
【相关文章】
  • ASP.NET Web服务如何工作?
  • eXo Platform开始从门户转向Web OS
  • MyEclipse 5.0 + WebLogic 9.2 配置详解
  • MyEclipse + WebLogic 开发 EJB 需注意
  • 初学web标准的几个误区【摘自-网站重构】
  • 菜鸟 Weblogic 9 之旅图文 1 - 下载和安装(Windows版本)
  • Web应用中的安全策略(一)阻止SQL注入
  • 关于运行web程序的几点错误处理方法
  • Apache Web Server 的配置 Mod_rewrite 模块静态化实例
  • xfire--开发WEB SERVICE的又一利器
  • 【随机文章】
  • 巧将Word页保存成图片
  • hao123网址大全之收藏版
  • 关闭端口
  • 关于校验和的计算
  • oracle9i在linux9下的安装
  • Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
  • sap mm后台配置学习-20060413
  • CTDP linux 程序员手册 C和C++编程(10)Linux mutex互斥和条件变量
  • 经典强大的服务器软件Apache
  • 没有超链接,在DREAMWEAVER中插入FLASH后
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.