but my object (Project) is a complex object. My above code does not run perfectly. it is showing 500 error code. I am trying to use ObjectContent for my complex object:-
{System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at BuilderProjects.Controllers.ProjectController.<Create>d__2.MoveNext() in c:\Users\priyank.saxena\Documents\Visual Studio 2012\Projects\BuilderProjects\BuilderProjects\Controllers\ProjectController.cs:line 87}
Recently, i have come to know that we can not read the complex object from body. we need to read that from uri. how can we do that ? I want to know that how can we call a webservice passing complex object into that. and also how can we read that in web api
method? i am new to this. plz help me with any example or any link for that.
public HttpResponseMessage PostProject([FromUri] Project project)
{
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, project);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = project.ProjectId }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
this is my web method that i am trying to call.
and i am using following method to call this service:-
public async Task<ActionResult> Create(Project projects)
{
try
{
HttpClient client = new HttpClient();
JavaScriptSerializer serializer = new JavaScriptSerializer();
String contentStr = serializer.Serialize(projects);
ObjectContent content = new ObjectContent(typeof(Project), projects, new JsonMediaTypeFormatter());
HttpResponseMessage putResponse = await client.PostAsync(serviceprovider + "project/postproject", content);
putResponse.EnsureSuccessStatusCode();
return RedirectToAction("Index");
}
catch (HttpRequestException e)
{
return View();
}
}
My object "Project" is complex object having another object of Builder in that.
Priyank Saxena
(Mark as Answer If you find helpful)
it is the line where i am ensuring my success status code:-
putResponse.EnsureSuccessStatusCode();
from where i am calling the web method; though, my web method was never called. that's why it is throwning exception.
see, i have two different projects. one is for client side from where i am calling the web service methods. and another is web api project where my web methods exists.
in both the projects, my model is same.
Priyank Saxena
(Mark as Answer If you find helpful)
priyankmtr
Contributor
2626 Points
526 Posts
problem in serializing the conplex object
Dec 31, 2012 04:47 AM|LINK
when my object is simple i can use following code without any problem:-
[HttpPost] public async Task<ActionResult> Create(Project projects) { try { HttpClient client = new HttpClient(); JavaScriptSerializer serializer = new JavaScriptSerializer(); String contentStr = serializer.Serialize(projects); StringContent content = new StringContent(contentStr, Encoding.UTF8, "application/json"); // ObjectContent content = new ObjectContent(typeof(Project), projects,new JsonMediaTypeFormatter()); HttpResponseMessage putResponse = await client.PostAsync(serviceprovider + "project/postproject", content); putResponse.EnsureSuccessStatusCode(); return RedirectToAction("Index"); } catch { return View(); } }but my object (Project) is a complex object. My above code does not run perfectly. it is showing 500 error code. I am trying to use ObjectContent for my complex object:-
[HttpPost] public async Task<ActionResult> Create(Project projects) { try { HttpClient client = new HttpClient(); JavaScriptSerializer serializer = new JavaScriptSerializer(); String contentStr = serializer.Serialize(projects); // StringContent content = new StringContent(contentStr, Encoding.UTF8, "application/json"); ObjectContent content = new ObjectContent(typeof(Project), projects,new JsonMediaTypeFormatter()); HttpResponseMessage putResponse = await client.PostAsync(serviceprovider + "project/postproject", content); putResponse.EnsureSuccessStatusCode(); return RedirectToAction("Index"); } catch { return View(); } }it is also not working fine. it is also showing 500 error code.
i know the problem is in line where i am creating ObjectContent object. can any one tell me where i am doing wrong ?
(Mark as Answer If you find helpful)
ignatandrei
All-Star
137663 Points
22144 Posts
Moderator
MVP
Re: problem in serializing the conplex object
Dec 31, 2012 08:18 AM|LINK
maybe there are circular references.
let the error flow or at least return the error ( catch(Exception ex) return Content(ex.Message) )
priyankmtr
Contributor
2626 Points
526 Posts
Re: problem in serializing the conplex object
Dec 31, 2012 09:33 AM|LINK
thanks for your reply.
it is showing me following httprequestexception:-
{System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at BuilderProjects.Controllers.ProjectController.<Create>d__2.MoveNext() in c:\Users\priyank.saxena\Documents\Visual Studio 2012\Projects\BuilderProjects\BuilderProjects\Controllers\ProjectController.cs:line 87}
Recently, i have come to know that we can not read the complex object from body. we need to read that from uri. how can we do that ? I want to know that how can we call a webservice passing complex object into that. and also how can we read that in web api method? i am new to this. plz help me with any example or any link for that.
public HttpResponseMessage PostProject([FromUri] Project project) { if (ModelState.IsValid) { db.Projects.Add(project); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, project); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = project.ProjectId })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }this is my web method that i am trying to call.
and i am using following method to call this service:-
public async Task<ActionResult> Create(Project projects) { try { HttpClient client = new HttpClient(); JavaScriptSerializer serializer = new JavaScriptSerializer(); String contentStr = serializer.Serialize(projects); ObjectContent content = new ObjectContent(typeof(Project), projects, new JsonMediaTypeFormatter()); HttpResponseMessage putResponse = await client.PostAsync(serviceprovider + "project/postproject", content); putResponse.EnsureSuccessStatusCode(); return RedirectToAction("Index"); } catch (HttpRequestException e) { return View(); } }My object "Project" is complex object having another object of Builder in that.
(Mark as Answer If you find helpful)
ignatandrei
All-Star
137663 Points
22144 Posts
Moderator
MVP
Re: problem in serializing the conplex object
Dec 31, 2012 10:01 AM|LINK
What's here?
priyankmtr
Contributor
2626 Points
526 Posts
Re: problem in serializing the conplex object
Dec 31, 2012 10:08 AM|LINK
it is the line where i am ensuring my success status code:-
from where i am calling the web method; though, my web method was never called. that's why it is throwning exception.
see, i have two different projects. one is for client side from where i am calling the web service methods. and another is web api project where my web methods exists.
in both the projects, my model is same.
(Mark as Answer If you find helpful)
priyankmtr
Contributor
2626 Points
526 Posts
Re: problem in serializing the conplex object
Jan 02, 2013 04:52 AM|LINK
is no one there who can help me with that ? All MVP's, please help.
(Mark as Answer If you find helpful)