ASP.NET: 通过 Web 方法获取当前 URL?

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

我正在尝试通过 Web 方法检索当前页面的 URL。下面的代码适用于 Page_Load 等普通 C# 方法,但不适用于 Web 方法。

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

我收到一个空字符串 ("") 作为 project_id。我做错了什么?

尝试执行下一个代码:

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

Url 只是对象,这就是为什么它 returns 对你来说是空值。 AbsoluteUri 将给出当前页面的完整 URL。示例:http://yourweb.site/Admin.aspx?id=15&time=yesterday

要获取客户对当前网站的预览请求的信息,您可以使用 UrlReferrer,如下所示:

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

你需要这个:

[WebMethod]
public static string mywebmethod()
{
string myUrl=  HttpContext.Current.Request.UrlReferrer.PathAndQuery.ToString();
return parameters
}