.net和js 获取当前url各种属性
转来
假设当前页完整地址是: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
網址: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/
Request.RawUrl /News/Press/Content.aspx/?id=
Request.Url.AbsolutePath /News/Press/Content.aspx/
Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port
Request.Url.Authority localhost:
Request.Url.LocalPath /News/Press/Content.aspx/
Request.PathInfo /
Request.Url.PathAndQuery /News/Press/Content.aspx/?id=
Request.Url.Query ?id=
Request.Url.Fragment
Request.Url.Segments 5个 []/
[]News/
[]Press/
[]Content.aspx/
[]
HttpContext.Current.Request.ApplicationPath
"/"
HttpContext.Current.Request.ServerVariables["Server_Name"]
"10.0.80.223"
HttpContext.Current.Request.ServerVariables["HTTPS"]
"off"
HttpContext.Current.Request.Url
{http://10.0.80.223:8081/api/LoanContract/GetContractPlayByContractCode?contractCode=C91642018051400011}
AbsolutePath: "/api/LoanContract/GetContractPlayByContractCode"
AbsoluteUri: "http://10.0.80.223:8081/api/LoanContract/GetContractPlayByContractCode?contractCode=C91642018051400011"
Authority: "10.0.80.223:8081"
DnsSafeHost: "10.0.80.223"
Fragment: ""
Host: "10.0.80.223"
HostNameType: IPv4
IdnHost: "10.0.80.223"
IsAbsoluteUri: true
IsDefaultPort: false
IsFile: false
IsLoopback: false
IsUnc: false
LocalPath: "/api/LoanContract/GetContractPlayByContractCode"
OriginalString: "http://10.0.80.223:8081/api/LoanContract/GetContractPlayByContractCode?contractCode=C91642018051400011"
PathAndQuery: "/api/LoanContract/GetContractPlayByContractCode?contractCode=C91642018051400011"
Port:
Query: "?contractCode=C91642018051400011"
Scheme: "http"
Segments: {string[]}
UserEscaped: false
UserInfo: ""
HttpContext.Current.Request.Url.Segments
{string[]}
[]: "/"
[]: "api/"
[]: "LoanContract/"
[]: "GetContractPlayByContractCode"
// 是否为SSL认证站点
string secure = HttpContext.Current.Request.ServerVariables["HTTPS"];
string httpProtocol = (secure == "on" ? "https://" : "http://");
// 服务器名称
string serverName = HttpContext.Current.Request.ServerVariables["Server_Name"];
// 应用服务名称
string applicationName = HttpContext.Current.Request.ApplicationPath;
return httpProtocol + "/" + serverName + "/" + applicationName;
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
.net和js 获取当前url各种属性的更多相关文章
- js 获取和设置css3 属性值的实现方法
众多周知 CSS3 增加了很多属性,在读写的时候就没有原先那么方便了. 如:<div style="left:100px"></div> 只考虑行间样式的话 ...
- js 获取当前页url网址信息
转载地址:js如何准确获取当前页面url网址信息 摘录: 举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window ...
- JS获取页面URL信息
下面我们举例一个URL,然后获得它的各个组成部分: http://i.cnblogs.com/EditPosts.aspx?opt=1 window.location.href (设置或获取整个 UR ...
- js 获取当前URL信息
document.location 这个对象包含了当前URL的信息 location.host 获取port号 location.hostname 设置或获取主机名称 location.href 设置 ...
- js获取页面url
设置或获取对象指定的文件名或路径. window.location.pathname例:http://localhost:8086/topic/index?topicId=361alert(windo ...
- js获取页面url的方法
我们可以用javascript获得其中的各个部分 1, window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) 本例返回值: http://ifisker.com/b ...
- js获取当前url信息
window.location 属性 描述 hash 设置或获取 href 属性中在井号"#"后面的分段. host 设置或获取 location 或 URL 的 hostname ...
- js获取当前url地址及参数
介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...
- js获取当前url
1.window.location.href(设置或获取整个 URL 为字符串) 2.window.location.protocol(设置或获取 URL 的协议部分) 3.window.locati ...
随机推荐
- Struts2学习第一天--Struts2的概述、Struts2的入门、Struts2常见的配置、Struts2的Action的编写
action的name要与访问路径对应.hello.action. 加到tomcat启动 访问:http://localhost:8080/struts2-1/demo1/demo1.jsp 改为su ...
- 航天独角兽Spacex
2018年2月7日下午3时45分,猎鹰重型火箭在位于卡纳维拉尔角的肯尼迪航天中心LC-39A平台顺利升空.火箭直升云霄,按照既定轨道持续升空,位于美国弗罗里达州卡纳维拉尔角的航天发射中心硝烟四起,非常 ...
- (原創) Gvim 個人習慣常用設定 (vim)
不定期更新這篇,因為查詢到好用的設定或者插件就會更新自己的設定. "set nocompatible let $LANG='zh_TW.UTF-8' set langmenu=zh_tw.u ...
- shared_ptr智能指针
来自博客:https://www.cnblogs.com/lzpong/p/6188034.html 多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_p ...
- hdu_1051 Wooden Sticks 贪心
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Call requires API level 11 (current min is 8)报错
新建一个Android Application Project,其中MainActivity.java中报错如下 Call requires API level 11(current min is 8 ...
- rest-assured的默认值与Specification重用
一.默认值 rest-assured发起请求时,默认使用的host为localhost,端口为8080,如果你想使用不同的端口,你可以这样做: given().port(80)...... 或者是简单 ...
- poj 1964 City Game
Bob is a strategy game programming specialist. In his new city building game the gaming environment ...
- bzoj 1189 二分+最大流
题目传送门 思路: 先预处理出每个人到每扇门的时间,用门作为起点进行bfs处理. 然后二分时间,假设时间为x,将每扇门拆成1到x,x个时间点,表示这扇门有几个时间点是可以出去的.对于一扇门,每个时间点 ...
- codeforces 1068d Array Without Local Maximums dp
题目传送门 题目大意:给出一个长度为n的数组,这个数组有的数是给出的,有的数是固定的,且范围都在[1,200]之间,要求这个数组中,每一个数字都小于等于 前后两个数字的最大值,求方案数mod p. 思 ...