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