if the user could not be created then postResponse will not be ensuring success code. how can i read the value from Response message ?
My api method :-
public HttpResponseMessage Post([FromBody]User user)
{
UserBusinessRules userBusiness = new UserBusinessRules();
string result = userBusiness.AddUser(user);
HttpStatusCode message = new HttpStatusCode();
if (result.Equals("Success"))
message = HttpStatusCode.Created;
else
message = HttpStatusCode.BadRequest;
return Request.CreateResponse(message, result);
}
in this, result is having reason, that why could not the user be created ?
any idea to read the customized error message from httpresponsemessage ?
Priyank Saxena
(Mark as Answer If you find helpful)
priyankmtr
Contributor
2626 Points
526 Posts
send customized error description fro web api
Jan 21, 2013 06:42 AM|LINK
i am having a client (mvc4) from where i am calling a web api method to add user.
[HttpPost] public async Task<ActionResult> Create(User user, List<string> rolesList) { try { HttpClient client = new HttpClient(); user.Roles = new List<Role>(); foreach (string roleSelected in rolesList) { user.Roles.Add(new Role() { RoleId = "", RoleName = roleSelected }); } user.Application = new BusinessEntities.Application(); user.Application.ApplicationName = AppConfig.ApplicationName; JavaScriptSerializer serializer = new JavaScriptSerializer(); String contentStr = serializer.Serialize(user); StringContent content = new StringContent(contentStr, Encoding.UTF8, "application/json"); HttpResponseMessage postResponse = await client.PostAsync(AppConfig.userUrl, content); postResponse.EnsureSuccessStatusCode(); return RedirectToAction("Index"); } catch (Exception) { } }if the user could not be created then postResponse will not be ensuring success code. how can i read the value from Response message ?
My api method :-
public HttpResponseMessage Post([FromBody]User user)
{
UserBusinessRules userBusiness = new UserBusinessRules();
string result = userBusiness.AddUser(user);
HttpStatusCode message = new HttpStatusCode();
if (result.Equals("Success"))
message = HttpStatusCode.Created;
else
message = HttpStatusCode.BadRequest;
return Request.CreateResponse(message, result);
}
in this, result is having reason, that why could not the user be created ?
any idea to read the customized error message from httpresponsemessage ?
(Mark as Answer If you find helpful)