クライアントから404エラーを返すPUTリクエストがあります。コードは次のようになります。
{
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