web程序中获取应用程序系统变量的方法( For.net 1.1)
2006-12-03 20:23
453 查看
获取系统变量在.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>
"
);
}
}
}
亦可用于 Web service
注意,以上方法获取IP未必真实。
注:本文主要是针对.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>
"
);
}
}
}
亦可用于 Web service
注意,以上方法获取IP未必真实。
相关文章推荐
- web程序中获取应用程序系统变量的方法( For.net 1.1)
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- 桌面程序集成web程序的方法【cs集成bs应用程序,使两者互相调用,互相传递事件及变量】
- ASP.NET网站中获取当前虚拟目录的应用程序目录的方法(转)
- 三种方法可以获取当前web程序目录物理路径
- "Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data" 解决方法
- Andorid WebView获取JS全局变量或者调用方法的返回值
- 检测到指定的WEB服务器运行的不是ASP.NET 1.1版,您无法运行ASP.NET应用程序或服务和创建ASP.NET程序时候发生错误 HTTP/1.0 500 Server Error的解决方法
- VC获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
- 获取系统变量的小小程序
- JAVA文件中获取路径及WEB应用程序获取路径方法
- VC获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
- C++程序获取系统信息方法
- 微信小程序获取手机系统信息的方法【附源码下载】
- Asp.net中获取应用程序完整Url路径的方法
- ASP.NET(v 1.1) 程序发布时如何带上IE WebControls
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- c#.net各种应用程序中获取文件路径的方法
- Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法
- vs.net的Web程序无法调试的解决方法之一