【问题标题】:ASP.NET: Getting the Current URL via a Web Method?ASP.NET:通过 Web 方法获取当前 URL?
【发布时间】:2016-02-13 08:26:11
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# asp.net web-services asp.net-web-api webmethod


    【解决方案1】:

    你需要这个:

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

    【讨论】:

      【解决方案2】:

      尝试执行下一个代码:

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

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

      【讨论】:

      【解决方案3】:

      要获取客户对当前网站的预览请求的信息,您可以使用 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-10
        • 2014-10-02
        • 2018-06-09
        • 1970-01-01
        • 2020-02-17
        • 1970-01-01
        • 2017-01-03
        • 1970-01-01
        相关资源
        最近更新 更多