I have created Web API and trying to consume it within MVC Web Application but the web api methods are calling twice for every request ,MVC application is requesting only one call to web api
What I have tried:
Web Api Method :
[HttpPost]
[Route("SaveClientDetails")]
public async Task<httpresponsemessage> AddNewClient([FromBody]usp_GetClientDetails client)
{
try
{
usp_GetClientDetails result = new usp_GetClientDetails();
await Task.Run(() =>
{
result = IClientRepository.InsertClientDetails(client);
});
return Request.CreateResponse(HttpStatusCode.OK, new { StatusCode = HttpStatusCode.OK, Status = "Success", Data = result });
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.NotFound, new { StatusCode = HttpStatusCode.InternalServerError, Status = "Failed", Data = ex.Message });
}
}
Client Calling Web API:
[HttpPost]
public ActionResult AddClient(ClientAddModel objcltModel)
{
var parameters = new usp_GetClientDetails
{
ClientMappedTo=Convert.ToInt32(objcltModel.ClientMappedTo),
UserName="",
FirstName=objcltModel.FirstName,
LastName= objcltModel.LastName,
EmailId= objcltModel.EmailId,
PrimaryContactNo= objcltModel.PrimaryContactNo,
SecondaryContactNo= objcltModel.SecondaryContactNo,
QRCode=Common.CreateQRCodeWithContact(objcltModel.PrimaryContactNo),
AadharNo= objcltModel.AadharNo,
PanNo= objcltModel.PanNo,
Pincode= objcltModel.Pincode,
City= objcltModel.City,
IsActive=true,
CreatedDate=DateTime.Now,
Deposit=objcltModel.Deposite,
Area1= objcltModel.Area1,
Area2= objcltModel.Area2,
FloorNo= Convert.ToInt32(objcltModel.FloorNo),
FlatNo= objcltModel.FlatNo,
BuildingName= objcltModel.BuildingName,
Rate=objcltModel.Rate
};
var result = WebAPIGeneric.GetAPIResponse(WebAPIMethods.SaveClientDetails, parameters, Method.POST, null);
if(result !=null)
{
if(result.Status=="Success")
{
ViewBag.Message = "Client Added Successfully";
}
}
ViewBag.Userlst = getUserList() == null ? new SelectList(string.Empty) : new SelectList(getUserList().ToList(), "UserId", "FullName");
return View("Index");
}
GetAPIResponseMethod :
public static ResponseModel GetAPIResponse(string Url, object data, Method method,Hashtable htable)
{
var client = new RestClient(Common.baseUrl);
var request = new RestRequest(Url, method);
if(data==null && htable !=null)
{
foreach(DictionaryEntry entry in htable)
{
request.AddParameter(entry.Key.ToString(),entry.Value.ToString());
}
}
else
{
request.AddJsonBody(JsonConvert.SerializeObject(data));
}
var x = client.Execute(request);
return JsonConvert.DeserializeObject<responsemodel>(client.Execute(request).Content);
}
Response Model Class:
public class ResponseModel
{
public int StatusCode { get; set; }
public string Status { get; set; }
public object Data { get; set; }
}