Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the URL of the current page in C# [duplicate]

Tags:

c#

asp.net

People also ask

What is URL in asp net?

A request URL is simply the URL a user enters into their browser to find a page on your web site. You use routing to define URLs that are semantically meaningful to users and that can help with search-engine optimization (SEO). By default, the Web Forms template includes ASP.NET Friendly URLs.


Try this :

string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx

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

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

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: http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
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.

http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx


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/"

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


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

use HttpContext.Current.Request.RawUrl


If you want to get

localhost:2806 

from

http://localhost:2806/Pages/ 

then use:

HttpContext.Current.Request.Url.Authority

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..