0

I have created web API that is working fine as i checked that, but the problem is when i call that web API in asp .net website here is a code below of calling web API

protected void btn_search_Click(object sender, EventArgs e)
{
    HClient.BaseAddress = new Uri("http://localhost:50653/");

    HClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    BoiMember obj = new BoiMember();
    obj.CustomerId = txt_customerid.Text.Trim();
    obj.AadhaarNo = txt_aadharno.Text.Trim();
    obj.CustomerName = txt_name.Text.Trim();
    obj.AccountNo = txt_accountno.Text.Trim();
    obj.MobileNo = txt_mobile.Text.Trim();
    obj.branchcd = Session["BranchCode"].ToString();
    obj.ZoneCode = Session["ZoneCode"].ToString();
    obj.Campcd = "1";
    obj.ind = 1;
    obj.SourceType = 2;
    obj.UserId = Session["UserName"].ToString();
    string uri = "api/BoiMember/GetRecord/";
    var response = HClient.GetAsync(uri+obj).Result;

    if (response.IsSuccessStatusCode)
    {
        var GetData = response.Content.ReadAsAsync<IEnumerable<BoiMember>>().Result;
        GvdRecords.DataSource = GetData;
        GvdRecords.DataBind();
    }
    else
    {

    }
}

Where in the API controller named BoiMemberController when I call this web API without parameters it works fine but as I Pass parameters I get the status code 404 Error not found. My web APIConfig.cs has a code

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate:  "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
2
  • Why are you doing uri+obj? That is not going to result in a valid url. What does it produce? Commented Jul 16, 2016 at 12:05
  • Even if you serialized obj to a string an used uri + '/' + obj it would not make much sense - how would you use it? Commented Jul 16, 2016 at 12:14

1 Answer 1

1

By default when you do [some object].ToString() the ToString method returns the objects type. So you are probably passing a string similar to api/BoiMember/GetRecord/BoiMember (not sure on the fully qualified type). You need to build up the uri using string formatting. Here is a basic example with 2 parameters:

var uri = string.Format("api/BoiMember/GetRecord/?customerId={0}&aadhaarNo={1}"
, txt_customerid.Text.Trim()
, txt_aadharno.Text.Trim());

This assumes that your parameters are query string parameters. If you have a web api with the parameter positioned inside the url then you need to change the structure of the string accordingly.

You should also have null checks if necessary and also if a parameter if empty you might not want to send it to the api.

Sign up to request clarification or add additional context in comments.

2 Comments

thanx @Igor this worked but can i pass object as a parameters
@AmitYadav - not in an GET or DELETE. With Post/Put you can but you must serialize them using a serializer (usually to JSON or XML). There are many tutorials on how to do this with HttpClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.