转来

假设当前页完整地址是: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各种属性的更多相关文章

  1. js 获取和设置css3 属性值的实现方法

    众多周知 CSS3 增加了很多属性,在读写的时候就没有原先那么方便了. 如:<div style="left:100px"></div> 只考虑行间样式的话 ...

  2. js 获取当前页url网址信息

    转载地址:js如何准确获取当前页面url网址信息 摘录: 举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window ...

  3. JS获取页面URL信息

    下面我们举例一个URL,然后获得它的各个组成部分: http://i.cnblogs.com/EditPosts.aspx?opt=1 window.location.href (设置或获取整个 UR ...

  4. js 获取当前URL信息

    document.location 这个对象包含了当前URL的信息 location.host 获取port号 location.hostname 设置或获取主机名称 location.href 设置 ...

  5. js获取页面url

    设置或获取对象指定的文件名或路径. window.location.pathname例:http://localhost:8086/topic/index?topicId=361alert(windo ...

  6. js获取页面url的方法

    我们可以用javascript获得其中的各个部分 1, window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) 本例返回值: http://ifisker.com/b ...

  7. js获取当前url信息

    window.location 属性 描述 hash 设置或获取 href 属性中在井号"#"后面的分段. host 设置或获取 location 或 URL 的 hostname ...

  8. js获取当前url地址及参数

    介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...

  9. js获取当前url

    1.window.location.href(设置或获取整个 URL 为字符串) 2.window.location.protocol(设置或获取 URL 的协议部分) 3.window.locati ...

随机推荐

  1. sqlite数据库文件查看

  2. CH收藏的书

    论语 道德经 墨子

  3. HSSFWorkbook导出表格使用

    HSSFWorkbook                      excel的文档对象 HSSFSheet                         excel的表单 HSSFRow      ...

  4. C#委托笔记

    参考: 第一篇:http://www.tracefact.net/tech/009.html 第二篇:http://www.tracefact.net/tech/029.html 1.委托相当于一种参 ...

  5. 【guava】对象处理

    一,equals方法 我们在开发中经常会需要比较两个对象是否相等,这时候我们需要考虑比较的两个对象是否为null,然后再调用equals方法来比较是否相等,google guava库的com.goog ...

  6. Flink生态与未来

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  7. Jenkins持续集成企业实战系列之Jenkins插件下载及邮件配置-----05

    注:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.    最初接触Jenkins也是由于公司需求,根据公司需求Java代码项目升级的.(公司是 ...

  8. POJ1358 Agri-Net

    题目链接 就是裸的最小生成树,复习一下. prim算法: G=(V,E),V是点集,E是边集 假设T=(U,TE)是最小生成树.U,TE初始化为空 首先从V中任取一点 假设取V1,然后U={V1},只 ...

  9. PHP批量给目录下所有的文件转换编码

    代码如下: function tree(&$arr_file, $directory, $dir_name=''){            $mydir = dir($directory);  ...

  10. alter table添加表约束

    翻阅了一下网上关于alter table添加表约束的资料,学习下,然后供自己以后使用. 仅仅供自己使用... 总结alter table ### add constraint ## 使用方法 添加表约 ...