. NET and JS get current URL various properties

Source: Internet
Author: User
Tags httpcontext hosting

Turn around.

Assuming the current page full address is: Http://www.test.com:80/aaa/bbb.aspx?id=5&name=kelli

"/HTTP" is the protocol name

"Www.test.com" is the domain name

"80" is the port number

"AAA" is the station name.

"Bbb.aspx" is the page name (file name)

"Id=5&name=kelli" is a parameter

"1" gets the full URL (protocol name + domain + site name + file name + parameter)

String url=request.url.tostring ();

Url=http://www.test.com/aaa/bbb.aspx?id=5&name=kelli

"2" gets the site name + page name + parameters:

String Url=request.rawurl;

(or string url=request.url.pathandquery;)

Url=/aaa/bbb.aspx?id=5&name=kelli

"3" Get Site name + page name:

String Url=httpcontext.current.request.url.absolutepath;

(or string url= HttpContext.Current.Request.Path;)

Url=aaa/bbb.aspx

"4" Gets the domain name:

String Url=httpcontext.current.request.url.host;

Url=www.test.com

"5" gets the parameter:

String Url= HttpContext.Current.Request.Url.Query;

Url=?id=5&name=kelli

"6" Gets the port:

int port = HttpContext.Current.Request.Url.Port;

Port = 80

Request.rawurl: Gets the URL information for the client request (not including the host and port)------>/default2.aspx 
Request.applicationpath: Gets the virtual path of an ASP. NET application on the server. ------>/ 
Request.currentexecutionfilepath: Gets the virtual path of the current request. ------>/default2.aspx 
Request.path: Gets the virtual path of the current request. ------>/default2.aspx 
Request.pathinfo: Take additional path information for a resource with a URL extension------> 
Request.PhysicalPath: Gets the physical file system path that corresponds to the requested 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; Gets the full physical path of the current page. For example,

F:\XFU. Nsqs\project\website\default.aspx
HttpContext.Current.Request.PhysicalApplicationPath; //Get the physical path of the current program to run

such as F:\XFU. Nsqs\project\website\
HttpContext.Current.Server.MapPath (@ "\"); This is the MapPath in the page. Same usage

Httpruntime.appdomainapppath//This is new found, very useful.

Another is used to handle calling DLL files in ASP. dll files if you want to know the working directory of the current Web site can be

To use
System.AppDomain.CurrentDomain.BaseDirectory

The physical path of the Web site on the server disk: Httpruntime.appdomainapppath
Virtual Program path: Httpruntime.appdomainappvirtualpath

HttpContext.Current.Request.ApplicationPath Virtual application root Path
HttpContext.Current.Server.MapPath (".") The current absolute path
The HttpContext.Current.Server.MapPath (HttpContext.Current.Request.ApplicationPath) system

root directory

Sb. Append (String. Format ("Virtual path of the current request: {0}", Server.HTMLEncode

(Request.currentexecutionfilepath)));
Sb. Append (String. Format ("Get the root directory path of the current application: {0}",

Server.HTMLEncode (Request.applicationpath));
Sb. Append (String. Format ("Virtual path of the current request: {0}", Server.HTMLEncode

(Request.filepath)));
Sb. Append (String. Format ("Virtual path of the current request: {0}", Server.HTMLEncode

(Request.path)));
Sb. Append (String. Format ("Gets the physical file system path of the root of the currently executing application:

{0} ", Server.HTMLEncode (Request.physicalapplicationpath)));
Sb. Append (String. Format ("Get the physical file system path corresponding to the requested URL: {0}",

Server.HTMLEncode (Request.physicalapplicationpath));

Virtual path of the current request:/adirectory/sample/responsehtml.aspx
Gets the root directory path of the current application:/adirectory
Virtual path of the current request:/adirectory/sample/responsehtml.aspx
Virtual path of the current request:/adirectory/sample/responsehtml.aspx
Gets the physical file system path of the root directory of the currently executing application: E:\Visual Studio 2005\
Gets the physical file system path that corresponds to the requested URL: E:\Visual Studio 2005\\adirectory\

Sb. Append (String. Format ("Get the full absolute path of the project: {0}",

System.AppDomain.CurrentDomain.BaseDirectory.ToString ()));
This property is available only after an attempt is made to load an assembly into this domain
Sb. Append (String. Format ("Gets the project, which is used by the Assembly resolver to probe dynamically created

Assembly: {0} ", System.AppDomain.CurrentDomain.DynamicDirectory));
Sb. Append (String. Format ("Gets the physical path to the application directory on the disk. : {0} ",

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath));
Sb. Append (String. Format ("Get the virtual root path of the application: {0}",

System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath));
Sb. Append (String. Format ("Gets the name of the site. : {0} ",

System.Web.Hosting.HostingEnvironment.SiteName));
Sb. Append (String. Format ("Gets the virtual path provider for this application. : {0} ",

System.Web.Hosting.HostingEnvironment.VirtualPathProvider));
Sb. Append (String. Format ("returns the physical file corresponding to the specified virtual path on the WEB server

Path. : {0} ", Server.MapPath (" sss.aspx "));

Method One: Use regular expressions to get the address bar parameters: (highly recommended, both practical and convenient!) )

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"));

Here's an example:

If the Address bar URL is: abc.html?id=123&url=http://www.maidq.com

So, but you use the above method to invoke: Alert (getquerystring ("url"));

A dialog box pops up: The content is http://www.maidq.com

If using: Alert (getquerystring ("id")), then the popup content is 123;

Of course, if you do not pass parameters, such as your address is abc.html back no parameters, the force output call results sometimes error:

So we have to add a judgment to determine if the argument we requested is empty, first assign the value to a variable:

var myurl=GetQueryString("url");if(myurl !=null && myurl.toString().length>1){   alert(GetQueryString("url"));}

This will not be an error!

Method Two: Traditional methods

<script type= "text/javascript";
function Urlsearch () &NBSP;
{
   var name,value; &NBSP;
   var str=location.href;//Get entire Address bar
   var num =str.indexof ("?") &NBSP;
   str=str.substr (num+1);//Get all Parameters   &NBSP; stringvar . substr ( start  [,&NBSP; length &NBSP;]

var arr=str.split ("&"); Each parameter is placed in the array
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 (); Instantiation of
alert (request.id);
</script>

For example, save this code as 1.html.

So I'm going to visit 1.html?id=test

At this point, I'm going to take the value of test.


called in HTML.
<script type= "Text/javascript" >
var a= "http://baidu.com";
</script>
<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 is the thing behind it.

</script>

Stringvar.substr (start [, length ]

Returns a substring of the specified length starting at the specified position.

Stringvar

Required option. The string literal or string object to extract the substring from.

Start

Required option. The starting position of the desired substring. The index of the first character in the string is 0.

Length

Options are available. The number of characters that should be included in the returned substring.

If length is 0 or negative, an empty string is returned. If this argument is not specified, the substring continues to the end of the stringvar.

Some related parameters are listed below:

Str.tolowercase () Convert to lowercase
Str.touppercase () string all converted to uppercase

URL: Uniform Resource Locator (Uniform Resource Locator, url)
The complete URL consists of the following parts:
Scheme://host:port/path?query#fragment
Scheme: Communication protocol
Common Http,ftp,maito, etc.

Host: Hosts
The server (computer) domain Name System (DNS) hostname or IP address.

Port: Port number
An integer, optionally, that omits the default port for using the scenario, such as HTTP, with the default port of 80.

Path: Paths
A string separated by 0 or more "/" symbols, typically used to represent a directory or file address on a host.

Query: Querying
Optional, for use with dynamic Web pages such as CGI, ISAPI, php/jsp/asp/asp. NET and other technical Web pages) pass parameters, can have multiple parameters, separated by the "&" symbol, the name and value of each parameter is separated by the "=" symbol.

Fragment: Pieces of information
String that specifies the fragment in the network resource. For example, there are multiple noun interpretations in a Web page, and you can use fragment to navigate directly to a noun interpretation. (also known as anchor points.)

For such a URL

Http://www.maidq.com/index.html?ver=1.0&id=6#imhere

We can use JavaScript to get every part of it.
1, Window.location.href
The entire URL string (in the browser is the full address bar)
This example returns the value: http://www.maidq.com/index.html?ver=1.0&id=6#imhere

2,window.location.protocol
The protocol portion of the URL
This example returns the value: http:

3,window.location.host
The host part of the URL
This example returns the value: Www.maidq.com

4,window.location.port
The port portion of the URL
If you are using the default 80 port (update: Even if you added: 80), the return value is not the default of 80 but the null character
This example returns the value: ""

5,window.location.pathname
The path portion of the URL (that is, the file address)
This example returns the value:/fisker/post/0703/window.location.html

6,window.location.search
Query (Parameters) Section
In addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to get the values of the arguments we believe in.
This example returns the value:? ver=1.0&id=6

7,window.location.hash
Anchor Point
This example returns the value: #imhere

. NET and JS get current URL various properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.