关于REST服务POST请求,下面是我编写的代码块,但它不起作用,404错误即将到来。
protected void Button1_Click(object sender, EventArgs e)
{
oRequest = (HttpWebRequest)WebRequest.Create("http://localhost:52811/FetchRecipe.svc/GetRecipe/5");
oRequest.Method = "POST";
oRequest.ContentType = "application/json";
string strRequest = "{'iRecipeId':'5'}";
oRequest.ContentLength = strRequest.Length;
MemoryStream mo = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strRequest));
using (Stream sr = oRequest.GetRequestStream())
{
mo.CopyTo(sr);
sr.Close();
}
oResponse = (HttpWebResponse)oRequest.GetResponse();
Stream oResponseStream = oResponse.GetResponseStream();
StreamReader sr1 = new StreamReader(oResponseStream);
string sResponse = sr1.ReadToEnd();
}
WCF合同接口
[OperationContract]
[WebInvoke(UriTemplate = "/GetRecipe")]
List<RecipeModal> GetRecipe(string iRecipeId);
数据合同
[DataContract]
public class RecipeModal
{
[DataMember]
public int RecipeId { get; set; }
[DataMember]
public string RecipeTitle { get; set; }
[DataMember]
public int Servings { get; set; }
[DataMember]
public int ReadyTime { get; set; }
}
答案 0 :(得分:0)
尝试将操作合同更改为以下内容,以便将url参数映射到函数参数:
[OperationContract]
[WebInvoke(UriTemplate = "GetRecipe/{iRecipeId}")]
List<RecipeModal> GetRecipe(string iRecipeId);