我想我的GET方法有问题,因为我在尝试运行一段客户端代码时没有返回任何内容。客户端GET请求不返回任何东西?
我GET操作的合同是这样的:
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "/Group/{TagName}")]
List<Group> GetGroupsCollection(string TagName);
public List<Group> GetGroupsCollection(string TagNames)
{
List<Group> groups = (from g in Groups
where
(from t in g.Tags where t.TagName == TagNames select t).Count() > 0
select g).ToList();
return groups;
}
现在我没有任何数据有,所以我必须手动添加组和标签从我的客户端来测试这一点,我然后尝试添加标签一组,我这样做,像这样:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/AddTagtoGroup/{group}/{tag}")]
void AddTagtoGroup(string group, string tag);
public void AddTagtoGroup(string group, string tag)
{
var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();
if (result != null)
{
result.Tags.Add(new Tag() { TagName = tag });
}
}
而且从客户端,这是这样完成的:
private void AddTagetoGroup_Click(object sender, EventArgs e)
{
string uriAddTagtoGroup = string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}", textBox6.Text, textBox7.Text);
byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
req.Method = "POST";
req.ContentType = "application/xml";
req.ContentLength = arr.Length;
Stream reqStrm = req.GetRequestStream();
reqStrm.Write(arr, 0, arr.Length);
reqStrm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
MessageBox.Show(resp.StatusDescription);
reqStrm.Close();
resp.Close();
}
我回来的消息是好的,一切似乎都很好。
现在这块客户端代码我得到一个问题是这样的:
string uriGetGroupsCollection = "http://localhost:8000/Service/GetGroupsCollection/{TagName}";
private void button8_Click(object sender, EventArgs e)
{
string tagUri = uriGetGroupsCollection.Replace("{TagName}", textBox8.Text);
XDocument xDoc = XDocument.Load(tagUri); //this line gives 404 error not found.
var Tag = xDoc.Descendants("Group")
.Select(n => new
{
Tag = n.Element("GroupName").Value,
})
.ToList();
dataGridView3.DataSource = Tag;
}
这是关系到我首先提到的GET操作。所以我不确定如何找出它的客户端代码是否做错了什么或者我的实际方法是什么GetGroupsCollection
?
因此,无论我的问题是有关添加标签到组:
public void AddTagtoGroup(string group, string tag)
{
var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();
if (result != null)
{
result.Tags.Add(new Tag() { TagName = tag });
}
}
或其相关为GetGroupsCollection
客户端代码?
我更新了我的问题,以反映我以前得到哪个surfen解决的小错误(404错误),但这没有解决我没有得到任何回报的问题?
希望这是好的,清晰,奠定了良好的任何问题,不要犹豫,问。 – 2012-04-07 16:34:36
您可以使用[Fiddler](http://www.fiddler2.com/)查看原始/文本客户端http请求/响应。 – Eugene 2012-04-07 16:45:20
您也可以使用Visual Studio调试器来破坏像AddTagtoGroup这样的方法中的代码执行,检查变量,检查列表内容和程序流程。 – surfen 2012-04-07 18:01:16