HttpContext.Current.Request.Url.ToString() 并不可靠。

如果当前URL为 
http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5

通过HttpContext.Current.Request.Url.ToString()获取到的却是

http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=¼¼Êõ

正确的方法是:HttpContext.Current.Request.Url.PathAndQuery

Request.Url.PathAndQuery

string 类型。等于 Request.Url.AbsolutePath 和 Request.Url.Query 相加。

Request.Url.AbsolutePath

string 类型。指当前页面 URL 的绝对路径,不包括查询字符串部分。

示例:/test/Default.aspx

Request.Url.Query

string 类型。指当前页面 URL 的查询字符串,从“?”开始。

示例:?a=1&b=2

1、通过ASP.NET获取
如果测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath:                /testweb
Request.CurrentExecutionFilePath:       /testweb/default.aspx
Request.FilePath:                       /testweb/default.aspx
Request.Path:                           /testweb/default.aspx
Request.PhysicalApplicationPath:        E:\WWW\testwebRequest.PhysicalPath:                   E:\WWW\testweb\default.aspx
Request.RawUrl:                         /testweb/default.aspx
Request.Url.AbsolutePath:               /testweb/default.aspx
Request.Url.AbsoluteUrl:                http://www.test.com/testweb/default.aspx
Request.Url.Host:                       www.test.com
Request.Url.LocalPath:                  /testweb/default.aspx

URL http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath /
Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath) Content.aspx
Request.CurrentExecutionFilePath /News/Press/Content.aspx
Request.FilePath /News/Press/Content.aspx
Request.Path /News/Press/Content.aspx/123
Request.RawUrl /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath /News/Press/Content.aspx/123
Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port 1897
Request.Url.Authority localhost:1897
Request.Url.LocalPath /News/Press/Content.aspx/123
Request.PathInfo /123
Request.Url.PathAndQuery /News/Press/Content.aspx/123?id=1
Request.Url.Query ?id=1
Request.Url.Fragment  
Request.Url.Segments

/
News/
Press/
Content.aspx/

123

2、通过JS获取

<table width=100% cellpadding=0 cellspacing=0 border=0 >

<script>

thisURL = document.URL;

thisHREF = document.location.href;

thisSLoc = self.location.href;

thisDLoc = document.location;

strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>"

strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>"

strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>"

strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>"

document.write( strwrite );

</script>

thisDLoc = document.location; <BR>

thisURL = document.URL; <BR>

thisHREF = document.location.href; <BR>

thisSLoc = self.location.href;<BR>

<script>

thisTLoc = top.location.href;

thisPLoc = parent.document.location;

thisTHost = top.location.hostname;

thisHost = location.hostname;

strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>"

strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>"

strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>"

strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>"

document.write( strwrite );

</script>

thisTLoc = top.location.href; <BR>

thisPLoc = parent.document.location; <BR>

thisTHost = top.location.hostname; <BR>

thisHost = location.hostname;<BR>

<script>

tmpHPage = thisHREF.split( "/" );

thisHPage = tmpHPage[ tmpHPage.length-1 ];

tmpUPage = thisURL.split( "/" );

thisUPage = tmpUPage[ tmpUPage.length-1 ];

strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>"

strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>"

document.write( strwrite );

</script><tr><td>

=================
获取IP
1、ASP.NET中获取

获取服务器的IP地址: 
using System.Net;

string myIP,myMac;
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; 
if ( addressList.Length>1) 
{
myIP = addressList[0].ToString(); 
myMac = addressList[1].ToString(); 

else 

myIP = addressList[0].ToString(); 
myMac = "没有可用的连接";

myIP地址就是服务器端的ip地址。

获取客户端的ip地址,可以使用

//获取登录者ip地址
string ip = Request.ServerVariables["REMOTE_ADDR"].ToString(); 
2、通过JS获取
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head>

<body>

<object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" ></object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" ></object>

<form name="myForm">
<br/>MAC地址:<input type="text" name="macAddress">
<br/>IP地址:<input type="text" name="ipAddress">
<br/>主机名:<input type="text" name="hostName">
</form>

</body>
</html>
<script language="javascript">
var sMacAddr="";
var sIPAddr="";
var sDNSName="";

var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');

</script>

<script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">
        if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
                          if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
                                        sIPAddr = objObject.IPAddress(0);
                          if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
                    sMacAddr = objObject.MACAddress;
                          if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
                                        sDNSName = objObject.DNSHostName;
         }
</script>

<script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript">

myForm.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
       myForm.hostName.value=sDNSName;
</script>

asp.net中获取当前url的方法的更多相关文章

  1. ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

    ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...

  2. asp.net后台获取路径的各种方法归纳

    asp.net后台获取路径的各种方法归纳   1.Request.CurrentExecutionFilePath    获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...

  3. ASP.NET中获取当日,当周,当月,当年的日期

     ASP.NET中获取当日,当周,当月,当年的日期 在ASP.NET开发中,经常会碰到要获取当日,当周,当月,当年的日期. 以下将源码贴出来和大家分享. aspx中代码如下: <table ce ...

  4. ASP.net中导出Excel的简单方法介绍

    下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...

  5. android中获取root权限的方法以及原理(转)

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...

  6. ASP.NET中XML转JSON的方法

    原文:ASP.NET中XML转JSON的方法 许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理.要实现这一点,它们必须将XML格式转换为JSON格式. X ...

  7. dede织梦栏目页和文章页中获取当前栏目名称方法

    一般情况下,在dede织梦系统中列表页.栏目页和文章页中获取当前所在栏目名称只需要代码:{dede:type}[field:typename]{/dede:type}即可,不需要定义ID,默认的就是当 ...

  8. Android中获取文件路径的方法总结及对照

    最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...

  9. LoadRunner中获取当前系统时间方法

    LoadRunner中获取当前系统时间方法 发表于:2017-6-02 11:41  作者:人生_0809   来源:51Testing软件测试网采编 字体:大 中 小 | 上一篇 | 下一篇 | 打 ...

随机推荐

  1. openssh for windows安装

     openssh for windows安装 2009-11-22 22:43:58 分类: WINDOWS 本文转自:http://blog.chinaunix.net/uid-7541208-id ...

  2. jQuery 学习之路(1):引子

    一.主流 javascript 库 除 jQuery 外,还有 Prototype.Dojo.YUI.ExtJS.MooTools ,其中 Prototype 较老,结构设计较为松散,ExtJS 界面 ...

  3. [译]git rebase

    rebase就是重新定义你分支的起点, 分支上的commit将生成对应的新的commit并放在你指定的新的起点commit后, 分支上的老commit将被删除. rebase就是将你的分支从一个com ...

  4. parastor2000挂载方式

    1.先授权 2.客户端安装使用0528parastor-client-centos6.6-38390.tar.xz clusconf -f fatnodes --sync-do "cd /m ...

  5. nginx的那些内置变量

    nginx在配置文件nginx.conf中可以使用很多内置变量,配置如下: location /info { add_header 'Content-Type' 'text/html'; echo & ...

  6. qstring与char*、基本数据类型的转换

    1.qstring转化为char* QString.toStdString.c_str() 2.char*转化为QString str = QString(QLatin1String(mm)); 3. ...

  7. 如何使用java自定义注解?demo

    1.Description.java package kzfy.bk.com; import java.lang.annotation.Documented; import java.lang.ann ...

  8. Vim编辑器运用的五个技巧

    导读 如今 Vim 是每个人最喜欢的 Linux 文本编辑器,也是开发者和系统管理者最喜爱的开源工具.大多数人只是熟悉Vim的最最基本的操作,只能在终端使用 Vim 修改文本,但是它并没有任何一个我想 ...

  9. unity3d教程游戏包含的一切文件导入资源

    http://www.58player.com/blog-2327-954.html 导入资源 将文件增加至工程文件夹的资源 (Assets) 文件夹后,Unity 将自动检测文件.将任何资源 (As ...

  10. Android学习笔记(二十)——自定义内容提供器

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 如果我们想要实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 Conten ...