zoukankan      html  css  js  c++  java
  • .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:ProjectsSolutionwebNewsPressContent.aspx 
    System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:ProjectsSolutionwebNewsPress 
    Request.PhysicalApplicationPath D:ProjectsSolutionweb 
    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  5个 [0]/
                                            [1]News/
                                            [2]Press/
                                            [3]Content.aspx/
                                            [4]123
    View Code
    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: 8081
        Query: "?contractCode=C91642018051400011"
        Scheme: "http"
        Segments: {string[4]}
        UserEscaped: false
        UserInfo: ""
    HttpContext.Current.Request.Url.Segments
    {string[4]}
        [0]: "/"
        [1]: "api/"
        [2]: "LoanContract/"
        [3]: "GetContractPlayByContractCode"
    View Code

    // 是否为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: empDefault2.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.NSQSprojectwebsiteDefault.aspx
    HttpContext.Current.Request.PhysicalApplicationPath; // 获得当前程序运行的物理路径比

    如F:XFU.NSQSprojectwebsite
    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

  • 相关阅读:
    HAOI2015 树上染色
    HAOI2010 软件安装
    T2 Func<in T1,out T2>(T1 arg)
    事无巨细
    LitJson JavaScriptSerializer
    数据库操作
    jQuery:总体掌握
    sql一个题的解法分析讲解
    Javascript系列:总体理解
    c#
  • 原文地址:https://www.cnblogs.com/love201314/p/9211827.html
Copyright © 2011-2022 走看看