Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 493 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the URL of the current page in C#

#1
Can anyone help out me in getting the URL of the current working page of ASP.NET in C#?
Reply

#2
if you just want the part between http:// and the first slash

string url = Request.Url.Host;

would return stackoverflow.com if called from this page

Here's the [complete breakdown](

[To see links please register here]

)
Reply

#3
Try this :

string url = HttpContext.Current.Request.Url.AbsoluteUri;
//

[To see links please register here]


string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx

string host = HttpContext.Current.Request.Url.Host;
// localhost
Reply

#4
A search landed me at this page, but it wasn't quite what I was looking for. Posting here in case someone else looking for what I was lands at this page too.

There is two ways to do it if you only have a string value.

.NET way:

Same as @Canavar, but you can instantiate a new Uri Object


String URL = "http://localhost:1302/TESTERS/Default6.aspx";
System.Uri uri = new System.Uri(URL);

which means you can use the same methods, e.g.

string url = uri.AbsoluteUri;
//

[To see links please register here]


string host = uri.host
// localhost


Regex way:

[To see links please register here]


Reply

#5
Just sharing as this was my solution thanks to Canavar's post.

If you have something like this:

"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"

or like this:

"https://www.something.com/index.html?a=123&b=4567"

and you only want the part that a user would type in then this will work:


String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");

which would result in these:

"http://localhost:1234/"
"https://www.something.com/"
Reply

#6
I guess its enough to return absolute path..

Path.GetFileName( Request.Url.AbsolutePath )

using System.IO;
Reply

#7
If you want to get

localhost:2806

from

[To see links please register here]


then use:

HttpContext.Current.Request.Url.Authority
Reply

#8
a tip for people who needs the path/url in global.asax file;

If you need to run this in **global.asax > Application_Start** and you app pool mode is **integrated** then you will receive the error below:

> **Request is not available in this context exception in
> Application_Start.**

In that case you need to use this:

> **System.Web.HttpRuntime.AppDomainAppVirtualPath**

Hope will help others..
Reply

#9
the request.rawurl will gives the content of current page
it gives the exact path that you required

use `HttpContext.Current.Request.RawUrl`
Reply

#10
You may at times need to get different values from URL.

Below example shows different ways of extracting different parts of URL

**EXAMPLE:** (Sample URL)

`http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2`

**CODE**

Response.Write("<br/>Host " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/>Authority: " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/>Port: " + HttpContext.Current.Request.Url.Port);
Response.Write("<br/>AbsolutePath: " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/>ApplicationPath: " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/>AbsoluteUri: " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/>PathAndQuery: " + HttpContext.Current.Request.Url.PathAndQuery);

**OUTPUT**

Host: localhost
Authority: localhost:60527
Port: 60527
AbsolutePath: /WebSite1test/Default2.aspx
ApplicationPath: /WebSite1test
AbsoluteUri:

[To see links please register here]

PathAndQuery: /WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2



You can copy paste above sample code & run it in asp.net web form application with different URL.

I also recommend reading ASP.Net Routing in case you may use ASP Routing then you don't need to use traditional URL with query string.

[To see links please register here]



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through