我想检索其GUID在Database中匹配的用户(类)的详细信息。
我在WCF
REST
项目中使用了ASP.NET
MVC
个完整的网络服务。
但是,WebResponse response =response .GetResponse()
在回复GUID
和获取user
数据时给出错误请求错误400。
这是Interface
:
[OperationContract]
[WebInvoke( Method="POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "Edit/{guid}"
)]
User Edit(string guid);
这是Controller Action
:
public ActionResult Edit(string guid)
{
if (guid != "0")
{
WebRequest Request = WebRequest.Create("http://localhost:65000/Service1.svc/Edit/guid");
// Set the Method property of the request to POST.
Request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = guid;
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
Request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
Request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = Request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = Request.GetResponse(); //THIS LINE GIVING BAD REQUEST ERROR 400
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
User UserData = JsonConvert.DeserializeObject<User>(responseFromServer);
ViewData["UserData"] = UserData;
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return View(UserData);
}
return View(new User());
}
P.S:以下代码工作正常,使用WebGet
检索所有用户。
我认为只有当我想要POST
一些像guid和read response
这样的数据时才会出现问题。
public ActionResult Index()
{
WebRequest Request = WebRequest.Create("http://localhost:65000/Service1.svc/RetrieveAll");
// Get the response.
WebResponse Response = Request.GetResponse();
// Get the stream containing content returned by the server.
Stream DataStream = Response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader Reader = new StreamReader(DataStream);
// Read the content.
string ResponseFromServer = Reader.ReadToEnd();
// Display the content.
List<User> UserData = JsonConvert.DeserializeObject<List<User>>(ResponseFromServer);
ViewData["UserData"] = UserData;
// Clean up the streams.
Reader.Close();
DataStream.Close();
Response.Close();
return View();
}
答案 0 :(得分:0)
似乎网址不正确
WebRequest Request = WebRequest.Create(&#34; http://localhost:65000/Service1.svc/Edit / guid &#34;);
应该是(使用guid in url)
WebRequest Request = WebRequest.Create(&#34; http://localhost:65000/Service1.svc/Edit/6F9619FF-8B86-D011-B42D-00CF4FC964FF&#34;);
答案 1 :(得分:0)
我认为问题在于你没有使用字符串guid作为变量而是作为文本,我认为正确的URL就是这个:
WebRequest request = WebRequest.Create("http://localhost:65000/Service1.svc/Edit/" + guid);
如果您使用&#34; guid&#34;在&#34;&#34;内它将被用作原样,一个单词,而不是变量guid中的值。 希望它有所帮助;)
答案 2 :(得分:0)
作者:
我发现在检索用户数据时问题出现在dB连接中,问题中提到的代码是正确的。