Dinuka Jayasuriya Dinuka Jayasuriya - 1 month ago 28
ASP.NET (C#) Question

ASP.NET: Getting the Current URL via a Web Method?

I'm trying to retrieve the URL of the current page via a Web Method. The code below works well on a normal C# Method such as the Page_Load but does not work inside a Web Method.

[WebMethod(EnableSession=true)]
public static void UpdateProjectName(string name)
{
string project_id = HttpContext.Current.Request.Url.ToString();
}


I'm receiving an empty string ("") as the project_id. What am I doing wrong?

Answer

To get information of the client's previews request to current website you can use the UrlReferrer as follow:

//To get the Absolute path of the URI use this
string myPreviousAbsolutePath = Page.Request.UrlReferrer.AbsolutePath;

//To get the Path and Query of the URI use this
string myPreviousPathAndQuery = Page.Request.UrlReferrer.PathAndQuery;