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

C# 调用我的Web API时请求无效,c#,asp.net-web-api,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,

我正在尝试从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
        };
    }
但我得到的回应是:

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

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

改为传递此消息:
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。

您的控制器名称是什么?我们正在调试,它是否命中控制器?@PraveenPaulose它是:公共类CustomerController:ApiController@BillMartin不是从浏览器,而是如果我从c#桌面创建一个httpclient,它将进入公共IEnumerable Get(Guid CompanyRef)出于某些原因,在您的
PostAsync
调用中,您将传递一个
HttpContent
实例,而不是传递
null
,请看另一个问题:嗨,比尔,谢谢您的回答。我已经更新了我的答案。我传递的变量是GUID。也许通过浏览器进行测试不是一个好主意:)不过应该可以在浏览器中使用。我推荐小提琴手或邮递员。非常适合webapi工作。嗨,比尔。对不起,如果这是一个空洞的问题(一直在看这个太久了,早该喝咖啡了),但路由的东西,这肯定看起来更干净。。。我是否只使用(在本例中)[Route(“Customer/{Add}/{CustomerRef}/{ContactNo}/etc”)?thanksIt将是[路由(“Customer/Add/{CustomerRef}/{ContactNo}/etc”)]。那么你的url应该是。如果你正在添加,你真的应该使用POST。然后呼叫服务/客户/添加并发送正文中的数据。创建一个与您的数据匹配的模型,并将其用作参数:Add([FromBody]ModelWithData model)如果您要添加,是否要用HttpPost装饰?@BillMartin是的,应该这样做。这就是为什么答案的最后一行。Andrew试图通过浏览器测试它,这就是为什么这个建议。@BillMartin解释了为什么挂了这么久:)@PraveenPoulose我已经按照你的建议做了ie[httpPost],但它仍然调用我的“Get”api吗(@AndrewSimpson我建议使用属性路由。确保调用正确的方法:
public IEnumerable<Services.Customer> Get(Guid CompanyRef)
"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"
[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
    };
}
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