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

C# ASP.NET:通过Web方法获取当前URL?,c#,asp.net,web-services,asp.net-web-api,webmethod,C#,Asp.net,Web Services,Asp.net Web Api,Webmethod,我正在尝试通过Web方法检索当前页面的URL。下面的代码在正常的C#方法(如页面加载)上运行良好,但在Web方法中不起作用 [WebMethod(EnableSession=true)] public static void UpdateProjectName(string name) { string project_id = HttpContext.Current.Request.Url.ToString(); } 我收到一个空字符串(“”)作为项目id。我做错了什么?尝试

我正在尝试通过Web方法检索当前页面的URL。下面的代码在正常的C#方法(如页面加载)上运行良好,但在Web方法中不起作用

[WebMethod(EnableSession=true)]
public static void UpdateProjectName(string name)
{
        string project_id = HttpContext.Current.Request.Url.ToString();
}
我收到一个空字符串(“”)作为项目id。我做错了什么?

尝试执行下一个代码:

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

Url
只是一个对象,所以它会向您返回空值
AbsoluteUri
将给出当前页面的完整URL。示例:

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

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

不。这将为我提供当前Web方法URL的地址。我得到了这个=>“”