如何使用asp.net在Web服务中获取所请求的URL?

use*_*423 9 c# asp.net web-services

我正在编写一个WebService,想要找出客户端用来调用我的WebMethod的URL.

好的..我会详细解释..

假设我有一个webservice(http://myWebservice/HashGenerator/HashValidator.asmx),如下所示

[WebMethod]
public string ValidateCode(string sCode)
{
  //need to check requested url here.The call will be coming from different sites
  //For example www.abc.com/accesscode.aspx
}
Run Code Online (Sandbox Code Playgroud)

请给我一个解决方案.

Ram*_*esh 18

如果您在.asmx webservice并需要获取当前网址,可以尝试以下方法.

HttpContext.Current.Request.Url
Run Code Online (Sandbox Code Playgroud)

  • 他为什么要获取当前的URL?他已经知道了. (2认同)

Dar*_*rov 5

你的问题不是很清楚.如果您尝试获取调用Web服务的ASPX页面的URL,则除非将其作为参数传递给Web方法或某些自定义HTTP标头,否则无法执行此操作.这是一个电话的例子:

var proxy = new YourWebServiceProxy();
string currentUrl = HttpContext.Current.Request.Url.ToString();
proxy.ValidateCode("some code", currentUrl);
Run Code Online (Sandbox Code Playgroud)

并且您的Web服务方法现在看起来像这样:

[WebMethod]
public string ValidateCode(string sCode, string callerUrl)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)


小智 5

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