如何在 C# 中定义和调用 api Post
How define and call an api Post in c#
我之前在 MVC 中创建过 POST/GET 请求。
在我的 HomeController 中
[HttpPost]
public string Index(int Value)
{
return Value.ToString();
}
并使用表单数据
设置 chrome 扩展 POSTMAN
我可以使用值为“1”的变量 'Value' 调用 http://localhost/mvcApp/
,并在 return
中获取字符串“1”
但是当我调用 http://localhost/mvcApp/api/survey/
时创建 surveyController : ApiController
不起作用
public string Post(int Value)
{
return Value.ToString();
}
"Message": "No HTTP resource was found that matches the request URI 'http://localhost/mvcApp/api/survey/'.",
"MessageDetail": "No action was found on the controller 'survey' that matches the request."
我不确定错误是在创建 api 的方式中,还是在 POSTMAN 尝试调用 api 的方式中。因为 '.'
也试试我的 HomeControler 索引
client.BaseAddress = new Uri("http://localhost/mvcApp");
var result = client.PostAsync("/api/survey", new
{
Value = 1
}, new JsonMediaTypeFormatter()).Result;
if (result.IsSuccessStatusCode) // here return Not found
WebApi 控制器的约定与普通 MVC 控制器的约定不同。
基本上,问题是您不能像以前那样指定 int
参数。
在你的 WebApi 控制器中试试这个:
// nested helper class
public class PostParams {
public int Value { get; set; }
}
public string Post(PostParams parameters) {
return parameters.Value.ToString();
}
看看它是如何工作的。
这是一篇关于在 POST 请求中向 WebAPI 控制器传递参数的详尽文章:
Passing-multiple-POST-parameters-to-Web-API-Controller-Methods
长话短说,这些是约定,粗略地说:
- 你不能捕获POST参数中的名称-值对
- 如果 class 是您的方法参数之一的参数类型,您可以在 class 的属性中捕获它们
- 你可以在方法参数中捕获查询参数
编辑
如果您希望使用 C# 测试您的 WebAPI 服务器,您可以按照以下步骤操作:
- 创建一个不错的控制台应用程序(最好在同一个解决方案中)
- 将 Web API Client NuGet 包添加到此控制台应用程序
- 让你的
Program.cs
做这样的事情。
以下代码使用 C# 5.0 async
and await
运算符。
它还使用 Task
class and anonymous types。
如果您对这些内容感兴趣,我已经指出了官方 MSDN 文章(单击链接)。
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
class Program {
public static void Main(string[] args) {
Test().Wait();
}
private static async Task Test() {
HttpClient client = new HttpClient();
await client.PostAsJsonAsync(
"http://localhost/mvcApp/api/survey/",
new {
value = 10
}
);
}
}
}
这并不容易。经过大量阅读后,我这样解决了。
首先api控制器需要用[FromBody]属性定义输入参数
// POST api/survey
public void Post([FromBody]string value)
{
}
为了测试,我在视图中放置了一个按钮并使用了 Ajax / Post,变量名在变量值之前需要是一个空字符串。
$(document).ready(
$('#post').click(function () {
var url = 'http://localhost/mvcApi/api/survey';
var data = { "": 'Hola' }; // Define a simple variable this way
$.ajax({
type: "POST",
url: url,
data: data,
success: sucess
}
});
})
或者如果你想发送多个值
data = { "": ["update one", "update two", "update three"] };
但是如果你想接收一个对象
public void Post(Survey data)
{
string value = data.Value.ToString();
}
$('#post').click(function () {
....
var data = { value: 25 }
这里有更多信息Sending Data and here Binding
我之前在 MVC 中创建过 POST/GET 请求。
在我的 HomeController 中
[HttpPost]
public string Index(int Value)
{
return Value.ToString();
}
并使用表单数据
设置 chrome 扩展 POSTMAN我可以使用值为“1”的变量 'Value' 调用 http://localhost/mvcApp/
,并在 return
但是当我调用 http://localhost/mvcApp/api/survey/
surveyController : ApiController
不起作用
public string Post(int Value)
{
return Value.ToString();
}
"Message": "No HTTP resource was found that matches the request URI 'http://localhost/mvcApp/api/survey/'.",
"MessageDetail": "No action was found on the controller 'survey' that matches the request."
我不确定错误是在创建 api 的方式中,还是在 POSTMAN 尝试调用 api 的方式中。因为 '.'
也试试我的 HomeControler 索引
client.BaseAddress = new Uri("http://localhost/mvcApp");
var result = client.PostAsync("/api/survey", new
{
Value = 1
}, new JsonMediaTypeFormatter()).Result;
if (result.IsSuccessStatusCode) // here return Not found
WebApi 控制器的约定与普通 MVC 控制器的约定不同。
基本上,问题是您不能像以前那样指定 int
参数。
在你的 WebApi 控制器中试试这个:
// nested helper class
public class PostParams {
public int Value { get; set; }
}
public string Post(PostParams parameters) {
return parameters.Value.ToString();
}
看看它是如何工作的。
这是一篇关于在 POST 请求中向 WebAPI 控制器传递参数的详尽文章: Passing-multiple-POST-parameters-to-Web-API-Controller-Methods
长话短说,这些是约定,粗略地说:
- 你不能捕获POST参数中的名称-值对
- 如果 class 是您的方法参数之一的参数类型,您可以在 class 的属性中捕获它们
- 你可以在方法参数中捕获查询参数
编辑
如果您希望使用 C# 测试您的 WebAPI 服务器,您可以按照以下步骤操作:
- 创建一个不错的控制台应用程序(最好在同一个解决方案中)
- 将 Web API Client NuGet 包添加到此控制台应用程序
- 让你的
Program.cs
做这样的事情。
以下代码使用 C# 5.0 async
and await
运算符。
它还使用 Task
class and anonymous types。
如果您对这些内容感兴趣,我已经指出了官方 MSDN 文章(单击链接)。
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
class Program {
public static void Main(string[] args) {
Test().Wait();
}
private static async Task Test() {
HttpClient client = new HttpClient();
await client.PostAsJsonAsync(
"http://localhost/mvcApp/api/survey/",
new {
value = 10
}
);
}
}
}
这并不容易。经过大量阅读后,我这样解决了。
首先api控制器需要用[FromBody]属性定义输入参数
// POST api/survey
public void Post([FromBody]string value)
{
}
为了测试,我在视图中放置了一个按钮并使用了 Ajax / Post,变量名在变量值之前需要是一个空字符串。
$(document).ready(
$('#post').click(function () {
var url = 'http://localhost/mvcApi/api/survey';
var data = { "": 'Hola' }; // Define a simple variable this way
$.ajax({
type: "POST",
url: url,
data: data,
success: sucess
}
});
})
或者如果你想发送多个值
data = { "": ["update one", "update two", "update three"] };
但是如果你想接收一个对象
public void Post(Survey data)
{
string value = data.Value.ToString();
}
$('#post').click(function () {
....
var data = { value: 25 }
这里有更多信息Sending Data and here Binding