0

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"
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

2 Answers2

1

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)

Bill Martin
  • 4,825
  • 9
  • 52
  • 86
  • Hi Bill, thanks for your answer. I have updated my answer. The variables I have passed in are GUIDs.. Perhaps testing via a browser was not such a great idea :) – Andrew Simpson Nov 09 '15 at 15:56
  • Should work in a browser though. I'd recommend either Fiddler or Postman. Great for webapi work. – Bill Martin Nov 09 '15 at 15:58
  • Hi Bill. Sorry if this is an inane question (been looking at this too long and way overdue a coffee) but the routing stuff which definitely looks cleaner... do I just use (in this case) [Route("Customer/{Add}/{CustomerRef}/{ContactNo}/etc etc etc")]? thanks – Andrew Simpson Nov 09 '15 at 16:25
  • It would be [Route("Customer/Add/{CustomerRef}/{ContactNo}/etc etc etc")]. Then your url would be http://service/customer/Add/(customerRef)/(CustomerNo). – Bill Martin Nov 09 '15 at 16:35
  • If you are adding, you really should use a POST. Then call service/customer/Add and send data in the body. Create a model that matches your data and use this as your param : Add([FromBody] ModelWithData model) – Bill Martin Nov 09 '15 at 16:39
  • hi, why 'service' (I have been playing around but not having much luck.Why am I so thick?) – Andrew Simpson Nov 09 '15 at 16:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94621/discussion-between-bill-martin-and-andrew-simpson). – Bill Martin Nov 09 '15 at 16:40
1

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.

Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19