Reputation: 7314
I am trying to call my web api from a c# client app
This is my API Controller server code:
public IEnumerable<Services.Customer> Get(Guid CompanyRef)
{
return customerRepository.Get(CompanyRef);
}
public Models.CustomerAddress Add(Guid CompanyRef, string ContactNo, Guid CustomerRef, string DOE, string Email, string FName, string SName,
Guid? addressRef, string add1, string add2, string add3, string town, string county, string pCode, string country)
{
var res= customerRepository.Add(CompanyRef, ContactNo, CustomerRef, DOE, Email, FName, SName,
addressRef, add1, add2, add3, town, county, pCode, country);
return new Models.CustomerAddress {
AddressRef =res.AddressRef,
CustomerRef =res.CustomerRef,
CustomerExists= (res.CustomerRef==CustomerRef)? true : false
};
}
by typing the uri directly into a browser I can test this.
http://myipaddress/api/Customer?CompanyRef=00000000-0000-0000-0000-00000000000&ContactNo=contactno2&CustomerRef=00000000-0000-0000-0000-000000000000&DOE=doe2&Email=email2&FName=fname2&SName=sname2&AddressRef=00000000-0000-0000-0000-000000000000&Add1=add1&Add2=add2&Add3=add3&Town=town&County=county&PCode=pcode&Country=country
but I get this response:
Error>
<Message>The request is invalid.</Message>
</Error>
I cannot see what I am doing wrong?
thanks
ADDITIONAL INFO
This is my code for calling it from a C# desktop client:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Shared.URL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Shared.HeaderType));
var response = client.PostAsync(route + "?" +
GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef + "&" +
GeneralTags.CONTACT_NO + "=" + customer.ContactNo + "&" +
GeneralTags.CUSTOMER_REF + "=" + customerLookUpResult.CustomerRef + "&" +
GeneralTags.DOE + "=" + customer.DOE + "&" +
GeneralTags.EMAIL + "=" + customer.Email + "&" +
GeneralTags.FNAME + "=" + customer.FName + "&" +
GeneralTags.SNAME + "=" + customer.SName + "&" +
GeneralTags.ADDRESS_REF + "=" + addressLookUpResult.AddressRef +
GeneralTags.ADD1 + "=" + customer.Add1 + "&" +
GeneralTags.ADD2 + "=" + customer.Add2 + "&" +
GeneralTags.ADD3 + "=" + customer.Add3 + "&" +
GeneralTags.TOWN + "=" + customer.Town + "&" +
GeneralTags.COUNTY + "=" + customer.County + "&" +
GeneralTags.PCODE + "=" + customer.PCode + "&" +
GeneralTags.COUNTRY + "=" + customer.Country
, null).Result;
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
var objs = JArray.Parse(json);
return JsonConvert.DeserializeObject<Model.CustomerAddress>(response.Content.ReadAsStringAsync().Result);
}
When I use this it goes into my:
public IEnumerable<Services.Customer> Get(Guid CompanyRef)
MODIFIED URI:L
This is my uri:
"http://uri/api/Customer/Add?CompanyRef=00000000-0000-0000-0000-000000000000&ContactNo=contactno2&CustomerRef=00000000-0000-0000-0000-000000000000&DOE=doe2&Email=email2&FName=fname2&SName=sname2&AddressRef=00000000-0000-0000-0000-000000000000Add1=add1&Add2=add2&Add3=add3&Town=town&County=county&PCode=pcode&Country=country"
Upvotes: 0
Views: 5333
Reputation: 5771
In your controller, you have a method called Add. This method is not decorated with the [HttpGet]. It looks like it is a POST method. You cannot call a POST method from the browser url in this way.
If you want it to be called add the attribute to the Add action
[HttpGet]
public Models.CustomerAddress Add(Guid CompanyRef, string ContactNo, Guid CustomerRef, string DOE, string Email, string FName, string SName,
Guid? addressRef, string add1, string add2, string add3, string town, string county, string pCode, string country)
{
var res= customerRepository.Add(CompanyRef, ContactNo, CustomerRef, DOE, Email, FName, SName,
addressRef, add1, add2, add3, town, county, pCode, country);
return new Models.CustomerAddress {
AddressRef =res.AddressRef,
CustomerRef =res.CustomerRef,
CustomerExists= (res.CustomerRef==CustomerRef)? true : false
};
}
Once you have done this, you will need to call it using the Url with the Add action specified
http://myipaddress/api/Customer/Add?CompanyRef=00000000-0000-0000-0000-00000000000&ContactNo=contactno2&CustomerRef=00000000-0000-0000-0000-000000000000&DOE=doe2&Email=email2&FName=fname2&SName=sname2&AddressRef=00000000-0000-0000-0000-000000000000&Add1=add1&Add2=add2&Add3=add3&Town=town&County=county&PCode=pcode&Country=country
If you need it to be a POST method, you could use POSTMAN to test your url.
Upvotes: 1
Reputation: 4943
Your signature doesn't match the controller. The model binder is expecting a GUID but you're passing much more.
Pass this instead: http://myipaddress/api/Customer?CompanyRef=(enter guid here)
Upvotes: 1