调用Web API时请求无效

本文关键字:请求 无效 API Web 调用 | 更新日期: 2023-09-27 18:21:09

我正试图从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"

调用Web API时请求无效

您的签名与控制器不匹配。模型绑定器需要一个GUID,但您传递的要多得多。

改为传递此消息:http://myipaddress/api/Customer?CompanyRef=(在此输入guid)

在控制器中,有一个名为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来测试你的url。