How to get current page URL in asp.net website using C#?

28 March 2022 | Viewed 8493 times

In this article, I will give some examples to how to get current page url using asp.net and c#. "Page" class will provide the properties to get all details about the current page like Page Name, Page Path (Virtual and Physical), Query Strings etc.,

If your website url is like this

http://www.venkateswarlu.net/dotnet/articles.aspx?query=test

Below are the list properties you can read from Page Class.
PropertyOutput
Page.AppRelativeTemplateSourceDirectory~/
Page.AppRelativeVirtualPath~/dotnet/articles.aspx
Page.Request.AppRelativeCurrentExecutionFilePath~/dotnet/articles.aspx
Page.Request.ApplicationPath/dotnet
Page.Request.CurrentExecutionFilePath/dotnet/articles.aspx
Page.Request.CurrentExecutionFilePathExtension.aspx
Page.Request.FilePath/dotnet/articles.aspx
Page.Request.Path/dotnet/articles.aspx
Page.Request.PhysicalApplicationPathC:\repo\project1\
Page.Request.PhysicalPathC:\repo\project1\dotnet\articles.aspx
Page.Request.RawUrl/dotnet/articles.aspx?query=test
Page.Request.Url.AbsolutePath/dotnet/articles.aspx
Page.Request.Url.AbsoluteUrihttp://www.venkateswarlu.net/dotnet/articles.aspx?query=test
Page.Request.Url.Hostwww.venkateswarlu.net
Page.Request.Url.IdnHostwww.venkateswarlu.net
Page.Request.Url.LocalPath/dotnet/articles.aspx
Page.Request.Url.OriginalStringhttp://www.venkateswarlu.net:8080/dotnet/articles.aspx?query=test
Page.Request.Url.PathAndQuery/dotnet/articles.aspx?query=test
Page.Request.Url.Query?query=test
Page.Request.UserHostAddress111.222.3.444

Examples
C# Code
Dictionary<string, string> dic = new Dictionary<string, string>();

dic.Add("Page.AppRelativeTemplateSourceDirectory", Page.AppRelativeTemplateSourceDirectory);
dic.Add("Page.AppRelativeVirtualPath", Page.AppRelativeVirtualPath);

dic.Add("Page.Request.AppRelativeCurrentExecutionFilePath", Page.Request.AppRelativeCurrentExecutionFilePath);
dic.Add("Page.Request.ApplicationPath", Page.Request.ApplicationPath);
dic.Add("Page.Request.CurrentExecutionFilePath", Page.Request.CurrentExecutionFilePath);
dic.Add("Page.Request.CurrentExecutionFilePathExtension", Page.Request.CurrentExecutionFilePathExtension);
dic.Add("Page.Request.FilePath", Page.Request.FilePath);
dic.Add("Page.Request.Path", Page.Request.Path);
dic.Add("Page.Request.PhysicalApplicationPath", Page.Request.PhysicalApplicationPath);
dic.Add("Page.Request.PhysicalPath", Page.Request.PhysicalPath);
dic.Add("Page.Request.RawUrl", Page.Request.RawUrl);

dic.Add("Page.Request.Url.AbsolutePath", Page.Request.Url.AbsolutePath);
dic.Add("Page.Request.Url.AbsoluteUri", Page.Request.Url.AbsoluteUri);
dic.Add("Page.Request.Url.Host", Page.Request.Url.Host);
dic.Add("Page.Request.Url.IdnHost", Page.Request.Url.IdnHost);
dic.Add("Page.Request.Url.LocalPath", Page.Request.Url.LocalPath);
dic.Add("Page.Request.Url.OriginalString", Page.Request.Url.OriginalString);
dic.Add("Page.Request.Url.PathAndQuery", Page.Request.Url.PathAndQuery);
dic.Add("Page.Request.Url.Query", Page.Request.Url.Query);

dic.Add("Page.Request.UserHostAddress", Page.Request.UserHostAddress);
dic.Add("Page.Request.UserHostName", Page.Request.UserHostName);

GridView1.DataSource = dic;
GridView1.DataBind();


Next