调用我的Web API时无效的请求

时间:2015-11-09 15:14:19

标签: c# asp.net-web-api

我正试图从c#客户端应用程序调用我的web api

这是我的API控制器服务器代码:

    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
        };
    }

通过直接在浏览器中输入uri,我可以对此进行测试。

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

但我得到了这样的答复:

Error>
<Message>The request is invalid.</Message>
</Error>

我看不出我做错了什么?

感谢

附加信息

这是我从C#桌面客户端调用它的代码:

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);
}

当我使用它时,它进入我的:

public IEnumerable<Services.Customer> Get(Guid CompanyRef)

修改后的URI:L

这是我的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"

2 个答案:

答案 0 :(得分:1)

您的签名与控制器不匹配。模型绑定器期待GUID,但你传递的更多。

改为通过: http://myipaddress/api/Customer?CompanyRef=(enter guid here)

答案 1 :(得分:1)

在您的控制器中,您有一个名为Add的方法。此方法未使用[HttpGet]进行修饰。看起来它是一个POST方法。您无法以这种方式从浏览器URL调用POST方法。

如果要调用它,请将该属性添加到“添加操作”

[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
    };
}

完成此操作后,您需要使用指定了添加操作

的Url来调用它
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

如果您需要它作为POST方法,可以使用POSTMAN来测试您的网址。