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. Linux查看系统信息的一些命令及查看已安装软件包的命令

    转自:http://cheneyph.iteye.com/blog/824746 系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看 ...

  2. winScp如何通过隧道代理进行远程连接

    目标机器:a  ip为192.168.2.150(内网ip)  a.xxx.x.xx(外网ip) 跳板机器:b ip为192.168.2.151(内网ip) b.xxx.xx.xx(外网ip) 目标机 ...

  3. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  4. mybaties # , $

    mybaties会对#引入的值加上双引号, 如: #{buildingName} -------->"buildingName";mybaties会将$引入的值直接显示到sq ...

  5. MySQL编译安装

    1.准备工作 其官方站点为http://www.mysql.com/ 为了避免发生端口冲突.程序冲突现象.建议先查询MySQL软件的安装情况,确认没有使用以RPM方式安装的mysql-server.m ...

  6. Redis学习笔记五:独立功能之事务

    Redis 事务提供了一种将多个命令请求打包,然后一次性.按顺序地执行多个命令的机制,并且在事务执行期间,服务器不会中断,会将事务中的所以命令都执行完毕才去处理其他客户端的命令请求. 事务的实现 事务 ...

  7. 在linux终端执行clear或top命令时出现:'xterm' unknown terminal type的错误

    例如: [root@localhost phpmyadmin]# clear 'xterm': unknown terminal type. 解决办法: 1.临时办法,下次启动失效,需要重新执行 ex ...

  8. 【转】phpcms基础内容

    <?php 思路: 一.目前在企业中使用比较多的cms内容管理有如下几种: 1.dedecms 2.phpcms 二.我们选择学习v9版本的phpcms,主要有以下几点原因: 1.基于MVC模式 ...

  9. CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 解题报告:中文题题意就不说了.还好数据不大,只有1000,枚举回文串的中心位置,然 ...

  10. iOS开发——网络篇——NSURLSession,下载、上传代理方法,利用NSURLSession断点下载,AFN基本使用,网络检测,NSURLConnection补充

    一.NSURLConnection补充 前面提到的NSURLConnection有些知识点需要补充 NSURLConnectionDataDelegate的代理方法有一下几个 - (void)conn ...