ASP.NET 网络服务
我可能是从错误的方向来处理这个问题的。我对 .net Web 服务相当陌生,正在寻求一些帮助。
我有一个在线的地理定位网络服务,我想将结果绑定到列表框或数据视图,但我也做不到。
我创建了一个名为 net.webservicex.www 的 Web 代理,它指向以下位置的 Web 服务: http:// www.webservicex.net/geoipservice.asmx
这是我的 C# 代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace web_services
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
net.webservicex.www.GeoIPService myProxy = new net.webservicex.www.GeoIPService(); // proxy object
string ipAddress, result;
ipAddress = txtIpAddress.Text;
result = myProxy.GetGeoIP("64.106.166.130");
lstResults.DataSource = result;
lstResults.DataMember = "IP";
}
}
}
我收到的错误是错误
无法隐式地将类型'web_services.net.webservicex.www.GeoIP'转换为第24行的'字符串'
如果有人能给我一些提示或想法那就太好了。
谢谢! 保罗
I may be going at this from the wrong direction. I'm fairly new to .net web services and was looking for a little help.
I have a geolocation webservice I got online and I wanted to bind the results to a listbox or a dataview but am unable too.
I've created a web proxy called net.webservicex.www that points to the webservice at.. http://www.webservicex.net/geoipservice.asmx
Here's my c# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace web_services
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
net.webservicex.www.GeoIPService myProxy = new net.webservicex.www.GeoIPService(); // proxy object
string ipAddress, result;
ipAddress = txtIpAddress.Text;
result = myProxy.GetGeoIP("64.106.166.130");
lstResults.DataSource = result;
lstResults.DataMember = "IP";
}
}
}
The error I'm recieving is Error
Cannot implicitly convert type 'web_services.net.webservicex.www.GeoIP' to 'string' at line 24
If someone could give me some tips or idea's that would be great.
Thanks!
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
<罢工>
您不需要将其放入结果字符串
由于您的Web服务返回的对象不可枚举,您可以通过将其放入可枚举类型来欺骗它:
You don't need to put it in the result string
Since the object returned by your webservice is not enumerable, You could trick it by putting it into an a enumerable Type:
查看此 Web 服务的 wsdl,似乎对 GetGeoIP 方法的调用返回一个对象,而不是字符串。
这就是编译器抱怨的原因。您可以将结果类型更改为其期望的对象,也可以使用 var 关键字。
Looking at the wsdl for this web service it seems that the call to the GetGeoIP method returns an object, not a string.
This is why the compiler is complaining. You could either change the type of result to the object it is expecting or use the var keyword.
此行有错误:
该方法返回的对象不是字符串,而是
web_services.net.webservicex.www.GeoIP
。您已将
result
声明为字符串,但该字符串不匹配。也许GeoIP
类上有一个ToString()
方法。如果是这样,您可以将代码更改为:并且根据
结果
的外观,您也许可以将其作为您的数据源。This line is at fault:
The object returned by that method is not a string, it's a
web_services.net.webservicex.www.GeoIP
.You've declared
result
as a string, which doesn't match. Perhaps there is aToString()
method on theGeoIP
class. If so, you could change your code to:And depending on what
result
looks like, you might just be able to make it your data source.我是 C# 和 .net 的初学者,但这是我解决这个问题的方法,我相信它会帮助像我这样的初学者:
结果对象看起来像这样:
很明显(是的,对......在吐血之后this...lol),结果不能是简单的STRING类型。
因此,从上述解决方案中获取样本,我将其制作如下:
Default.aspx.cs:
其中 - “mygeoip” 是我的 WebService 名称(而不是“net”) .webservicex.www")
GeoIPService 是我的命名空间。
Default.aspx:
就是这样 - 我希望我能帮助像我这样的初学者:)
I'm a very beginner with the C# and .net but here's how I solved this and I'm sure it'll help beginners like me:
The result object looks like this:
So obviously (yeah right...after spitting blood on this...lol), the result cannot be a simple STRING type.
So, taking samples from the above solutions I've made it like that:
Default.aspx.cs:
Where - "mygeoip" is my WebService name (instead of "net.webservicex.www")
and GeoIPService is my namespace.
Default.aspx:
That's it - I hope I've helped beginners like me :)