消费与自定义名称的Web API哈氏操作方法 [英] Consume Web API haing Action methods with custom name

查看:170
本文介绍了消费与自定义名称的Web API哈氏操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Web API中的操作方法名custome像CreateCustomer(..),GetCustomer(...)。这些被标注为[HttpPost] / [HTTPGET]。我不知道如何使用HttpClient的消耗它()。如果设置为以下和我打个电话

I am trying to consume web api in which action method names are custome like CreateCustomer(..), GetCustomer(...). These are annotated with [HttpPost]/[HttpGet]. I am not sure how to consume it using HttpClient(). If is set as following and me a call

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://host/directory/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/customer", cutomerObj).Result;

它抛出404在哪里,如果我使用Fiddler和发送arequest到的http://主机/目录/ API /客户使用JSON Customer对象它完美

It throws 404. Where as if i use fiddler and send arequest to http://host/directory/api/customer with JSON customer object it works perfectly

正好我失去了什么吗?我需要的用户POST和GET方法作为?

What exactly am i missing here? Do i need to user POST and GET as methods?

推荐答案

您需要配置你的路线,包括动作 API / {控制器} / {行动} / {ID} 和拨打电话如 API /客户/ CreateCustomer

you need to configure your route to include the action like api/{controller}/{action}/{id} and make call like api/customer/CreateCustomer

从C#,

var t = new HttpClient().GetAsync("http://localhost:63154/api/UserApi/CreateCustomer").Result.Content.ReadAsStringAsync().Result;

这篇关于消费与自定义名称的Web API哈氏操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆