我正在尝试从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"发布于 2015-11-09 23:53:55
您的签名与控制器不匹配。模型绑定器需要GUID,但您传递的GUID要多得多。
取而代之的是:http://myipaddress/api/Customer?CompanyRef=(enter
发布于 2015-11-10 00:00:25
在您的控制器中,有一个名为Add的方法。此方法不使用HttpGet修饰。它看起来像是一个POST方法。不能以这种方式从浏览器url调用POST方法。
如果希望调用它,请将该属性添加到add操作中
[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来测试你的url。
https://stackoverflow.com/questions/33612220
复制相似问题