用于WCF Web服务的PUT方法的“404”?

时间:2012-04-14 17:48:48

标签: c# wcf web-services rest http-status-code-404

我有一个PUT请求,它从我的客户端返回404错误,代码如下所示:

    {
        string uriupdatestudent = string.Format("http://localhost:8000/Service/Student/{0}/{1}/{2}", textBox16.Text, textBox17.Text, textBox18.Text);
        byte[] arr = Encoding.UTF8.GetBytes(uriupdatestudent);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriupdatestudent);
        req.Method = "PUT";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        using (Stream reqStrm = req.GetRequestStream())
        {
            reqStrm.Write(arr, 0, arr.Length);
            reqStrm.Close();
        }
        using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
        {
            MessageBox.Show(resp.StatusDescription);
            resp.Close();
        }
    }

OperationContract和Service如下所示:

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student")]
    void UpdateStudent(Student student);

    public void UpdateStudent(Student student) 
    {
        var findStudent = students.Where(s => s.StudentID == student.StudentID).FirstOrDefault();

        if (findStudent != null)
        {
            findStudent.FirstName = student.FirstName;
            findStudent.LastName = student.LastName;
        }

    }
[DataContract(Name="Student")]
public class Student
{
    [DataMember(Name = "StudentID")]
    public string StudentID { get; set; }
    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }
    [DataMember(Name = "LastName")]
    public string LastName { get; set; }
    [DataMember(Name = "TimeAdded")]
    public DateTime TimeAdded;
    public string TimeAddedString

2 个答案:

答案 0 :(得分:1)

因此,为了回答我的问题,我必须做两件事:

我必须更改我的操作合同,以便它可以输入字符串studentID,然后我可以delcare学生集合。

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student/{studentID}")]
    void UpdateStudent(string studentID, Student student);

    public void UpdateStudent(string studentID, Student student) 
    {
        var findStudent = students.Where(s => s.StudentID == studentID).FirstOrDefault();

        if (findStudent != null)
        {
            findStudent.FirstName = student.FirstName;
            findStudent.LastName = student.LastName;
        }

    }

然后从客户端我不得不回到使用字符串构建器方法,以便将集合作为xml发送。

    {
        string uriupdatestudent = string.Format("http://localhost:8000/Service/Student/{0}", textBox16.Text);
        StringBuilder sb = new StringBuilder();
        sb.Append("<Student>");
        sb.AppendLine("<FirstName>" + this.textBox17.Text + "</FirstName>");
        sb.AppendLine("<LastName>" + this.textBox18.Text + "</LastName>");
        sb.AppendLine("</Student>");
        string NewStudent = sb.ToString();
        byte[] arr = Encoding.UTF8.GetBytes(NewStudent);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriupdatestudent);
        req.Method = "PUT";
        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();
    }

有一个人在此之前提出了答案,他是对的,所以我要感谢你,你的答案将被接受! (如果没有删除)

答案 1 :(得分:0)

根据您正在调用的uri,您的服务能够解决它,因为传递了额外的路由信息​​吗?

您可以尝试更新服务方法签名以接受并映射即将到来的uri参数:

[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student/{fname}/{lname}/{id}")]
    void UpdateStudent(string fname, string lname, string id);

否则,您可以将客户端上的Student对象序列化为XML,并将其与请求正文中的请求一起发送。在这种情况下,您只需向http://localhost:8000/Service/Student发出请求,WCF会将传入请求的正文反序列化为相应的Student对象。