转来
假设当前页完整地址是: http://www.test.com:80/aaa/bbb.aspx?id=5&name=kelli
"http://" 是协议名
"www.test.com" 是域名
"80" 是端口号
"aaa" 是站点名
"bbb.aspx" 是页面名(文件名)
"id=5&name=kelli" 是参数
[1] 获取 完整 url (协议名 + 域名 + 站点名 + 文件名 + 参数)
- string url=Request.Url.ToString();
- url=http://www.test.com/aaa/bbb.aspx?id=5&name=kelli
[2] 获取 站点名 + 页面名 + 参数:
- string url=Request.RawUrl;
- (或 string url=Request.Url.PathAndQuery;)
- url=/aaa/bbb.aspx?id=5&name=kelli
[3] 获取 站点名 + 页面名:
- string url=HttpContext.Current.Request.Url.AbsolutePath;
- (或 string url= HttpContext.Current.Request.Path;)
- url=aaa/bbb.aspx
[4] 获取 域名:
- string url=HttpContext.Current.Request.Url.Host;
- url=www.test.com
[5] 获取 参数:
- string url= HttpContext.Current.Request.Url.Query;
- url=?id=5&name=kelli
[6] 获取 端口:
- int port = HttpContext.Current.Request.Url.Port;
- port = 80
Request.RawUrl: 获取客户端请求的 URL 信息(不包括主机和端口)------>/Default2.aspx
Request.ApplicationPath: 获取服务器上 ASP.NET 应用程序的虚拟路径.------>/
Request.CurrentExecutionFilePath: 获取当前请求的虚拟路径.------>/Default2.aspx
Request.Path: 获取当前请求的虚拟路径.------>/Default2.aspx
Request.PathInfo: 取具有 URL 扩展名的资源的附加路径信息 ------>
Request.PhysicalPath: 获取与请求的 URL 相对应的物理文件系统路径.------>E:\temp\Default2.aspx
- Request.Url.LocalPath:------>/Default2.aspx
- Request.Url.AbsoluteUri:------>http://localhost:8080/Default2.aspx
- Request.Url.AbsolutePath:---------------------------->/Default2.aspx
- HttpContext.Current.Request.PhysicalPath; // 获得当前页面的完整物理路径. 比如
- F:\XFU.NSQS\project\website\Default.aspx
- HttpContext.Current.Request.PhysicalApplicationPath; // 获得当前程序运行的物理路径比
如 F:\XFU.NSQS\project\website\
HttpContext.Current.Server.MapPath(@"\"); 这个就是在页面中的 MapPath 了. 一样用法
HttpRuntime.AppDomainAppPath // 这个是新发现的, 很好用.
还有一个是用来处理在 asp.net 中调用 dll 文件, 而 DLL 文件如果想知道当前的 web 站点的工作目录可
以用
System.AppDomain.CurrentDomain.BaseDirectory
网站在服务器磁盘上的物理路径: HttpRuntime.AppDomainAppPath
虚拟程序路径: HttpRuntime.AppDomainAppVirtualPath
HttpContext.Current.Request.ApplicationPath 虚拟应用程序根路径
HttpContext.Current.Server.MapPath(".")当前的绝对路径
HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath)系统的
根目录
- sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.htmlEncode
- (Request.CurrentExecutionFilePath)));
- sb.Append(string.Format("获取当前应用程序的根目录路径: {0}",
- Server.HtmlEncode(Request.ApplicationPath)));
- sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode
- (Request.FilePath)));
- sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode
- (Request.Path)));
sb.Append(string.Format(" 获取当前正在执行的应用程序的根目录的物理文件系统路径:
- {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));
- sb.Append(string.Format("获取与请求的 URL 相对应的物理文件系统路径: {0}",
- Server.HtmlEncode(Request.PhysicalApplicationPath)));
当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
获取当前应用程序的根目录路径: /aDirectory
当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
获取当前正在执行的应用程序的根目录的物理文件系统路径: E:\Visual Studio 2005\
获取与请求的 URL 相对应的物理文件系统路径: E:\Visual Studio 2005\\aDirectory\
- sb.Append(string.Format("获取项目完整的绝对路径: {0}",
- System.AppDomain.CurrentDomain.BaseDirectory.ToString()));
- // 仅在尝试向此域中加载程序集之后, 此属性才可用
sb.Append(string.Format(" 获取项目, 它由程序集冲突解决程序用来探测动态创建的
程序集: {0}", System.AppDomain.CurrentDomain.DynamicDirectory));
- sb.Append(string.Format("获取磁盘上指向应用程序目录的物理路径.: {0}",
- System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath));
- sb.Append(string.Format("获取应用程序的虚拟根路径: {0}",
- System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath));
- sb.Append(string.Format("获取站点的名称.: {0}",
- System.Web.Hosting.HostingEnvironment.SiteName));
- //sb.Append(string.Format("获取此应用程序的虚拟路径提供程序.: {0}",
- System.Web.Hosting.HostingEnvironment.VirtualPathProvider));
sb.Append(string.Format(" 返回与 Web 服务器上的指定虚拟路径相对应的物理文件
路径.: {0}", Server.MapPath("sss.aspx")));
方法一: 采用正则表达式获取地址栏参数:( 强烈推荐, 既实用又方便!)
- function GetQueryString(name)
- {
- var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if(r!=null)return unescape(r[2]); return null;
- }
- // 调用方法
- alert(GetQueryString("参数名 1"));
- alert(GetQueryString("参数名 2"));
- alert(GetQueryString("参数名 3"));
下面举一个例子:
若地址栏 URL 为: abc.html?id=123&url=http://www.maidq.com
那么, 但你用上面的方法去调用: alert(GetQueryString("url"));
则会弹出一个对话框: 内容就是 http://www.maidq.com/
如果用: alert(GetQueryString("id")); 那么弹出的内容就是 123 啦;
当然如果你没有传参数的话, 比如你的地址是 abc.html 后面没有参数, 那强行输出调用结果有的时候会报错:
所以我们要加一个判断 , 判断我们请求的参数是否为空, 首先把值赋给一个变量:
- var myurl=GetQueryString("url");
- if(myurl !=null && myurl.toString().length>1)
- {
- alert(GetQueryString("url"));
- }
这样就不会报错了!
方法二: 传统方法
- <script type="text/javascript">
- function UrlSearch()
- {
- var name,value;
- var str=location.href; // 取得整个地址栏
- var num=str.indexOf("?")
- str=str.substr(num+1); // 取得所有参数
- stringvar.substr(start [, length ]
- var arr=str.split("&"); // 各个参数放到数组里
- for(var i=0;i <arr.length;i++){
- num=arr[i].indexOf("=");
- if(num>0){
- name=arr[i].substring(0,num);
- value=arr[i].substr(num+1);
- this[name]=value;
- }
- }
- }
- var Request=new UrlSearch(); // 实例化
- alert(Request.id);
- </script>
比如说把这个代码存为 1.html
那么我要访问 1.html?id=test
这个时候就取到 test 的值了
在 html 里调用
- <script type="text/javascript">
- var a="http://baidu.com";
- </script>
- </head>
- <body>
- <a id="a1" href="">sadfsdfas</a>
- <script>
- var a1=document.getElementById("a1");
- a1.href=a;
- </script>
- <script type="text/javascript">
- var a="http://xxx.com/gg.htm?cctv";
- var s=a.indexOf("?");
- var t=a.substring(s+1);// t 就是? 后面的东西了
- </script>
- stringvar.substr(start [, length ]
返回一个从指定位置开始的指定长度的子字符串.
stringvar
必选项. 要提取子字符串的字符串文字或 String 对象.
start
必选项. 所需的子字符串的起始位置. 字符串中的第一个字符的索引为 0.
length
可选项. 在返回的子字符串中应包括的字符个数.
如果 length 为 0 或负数, 将返回一个空字符串. 如果没有指定该参数, 则子字符串将延续到 stringvar 的最后.
下面列举出一些相关的参数:
str.toLowerCase() 转换成小写
str.toUpperCase() 字符串全部转换成大写
URL 即: 统一资源定位符 (Uniform Resource Locator, URL)
完整的 URL 由这几个部分构成:
scheme://host:port/path?query#fragment
scheme: 通信协议
常用的 http,ftp,maito 等
host: 主机
服务器 (计算机) 域名系统 (DNS) 主机名或 IP 地址.
port: 端口号
整数, 可选, 省略时使用方案的默认端口, 如 http 的默认端口为 80.
path: 路径
由零或多个 "/" 符号隔开的字符串, 一般用来表示主机上的一个目录或文件地址.
query: 查询
可选, 用于给动态网页 (如使用 CGI,ISAPI,PHP/JSP/ASP/ASP.NET 等技术制作的网页) 传递参数, 可有多个参数, 用 "&" 符号隔开, 每个参数的名和值用 "=" 符号隔开.
fragment: 信息片断
字符串, 用于指定网络资源中的片断. 例如一个网页中有多个名词解释, 可使用 fragment 直接定位到某一名词解释.(也称为锚点.)
对于这样一个 URL
http://www.maidq.com/index.html?ver=1.0&id=6#imhere
我们可以用 javascript 获得其中的各个部分
1, window.location.href
整个 URl 字符串(在浏览器中就是完整的地址栏)
本例返回值: http://www.maidq.com/index.html?ver=1.0&id=6#imhere
2,window.location.protocol
URL 的协议部分
本例返回值: http:
3,window.location.host
URL 的主机部分
本例返回值: www.maidq.com
4,window.location.port
URL 的端口部分
如果采用默认的 80 端口(update: 即使添加了: 80), 那么返回值并不是默认的 80 而是空字符
本例返回值:""
5,window.location.pathname
URL 的路径部分(就是文件地址)
本例返回值:/fisker/post/0703/window.location.html
6,window.location.search
查询 (参数) 部分
除了给动态语言赋值以外, 我们同样可以给静态页面, 并使用 javascript 来获得相信应的参数值
本例返回值:?ver=1.0&id=6
7,window.location.hash
锚点
本例返回值:#imhere
来源: http://www.bubuko.com/infodetail-2654192.html