簡體   English   中英

將“ 404”用於WCF Web服務的PUT方法?

[英]“404” for PUT method to WCF web service?

我有一個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

因此,為了回答我的問題,我必須做兩件事:

我必須更改我的運營合同,以便可以使用輸入字符串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();
    }

在此之前有人回答過,他是正確的,因此我要感謝您,您的回答將被接受! (如果未刪除)

根據您呼叫的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對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM