Call Api Controller PostAsJsonAsync 简单类型参数
Call Api Controller PostAsJsonAsync simple type parameter
我正在使用 PostAsJsonAsync
.
调用 MVC Api 控制器
如果我想传递像 Class 实例这样的复杂类型,它工作正常。但我需要将简单类型传递为 string
,或 int
,如
HttpResponseMessage response = await
client.PostAsJsonAsync("http://localhost:62536/api/Controller/Method", "HELLO");
遇到错误404 Method
未找到。
我的网站Api控制器看起来像这样
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我的 Api 控制器方法如下所示。
public void Method(string id){}
如果尝试使用 Int
类型的另一个 Mehtod,它也不起作用..
HttpResponseMessage response = await
client.PostAsJsonAsync("http://localhost:62536/api/Controller/MethodA", 123);
public void MethodA(int id){}
如果我将该值分配给复杂类型,如 Class.. 它工作正常。
PostAsJsonAsync 仅适用于复杂类型?它如何让它发挥作用?
看起来像是解析问题。首先创建字符串变量然后传递给方法。
string x = (string)value;
public void Method([FromBody] string id){}
我正在使用 PostAsJsonAsync
.
如果我想传递像 Class 实例这样的复杂类型,它工作正常。但我需要将简单类型传递为 string
,或 int
,如
HttpResponseMessage response = await
client.PostAsJsonAsync("http://localhost:62536/api/Controller/Method", "HELLO");
遇到错误404 Method
未找到。
我的网站Api控制器看起来像这样
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我的 Api 控制器方法如下所示。
public void Method(string id){}
如果尝试使用 Int
类型的另一个 Mehtod,它也不起作用..
HttpResponseMessage response = await
client.PostAsJsonAsync("http://localhost:62536/api/Controller/MethodA", 123);
public void MethodA(int id){}
如果我将该值分配给复杂类型,如 Class.. 它工作正常。
PostAsJsonAsync 仅适用于复杂类型?它如何让它发挥作用?
看起来像是解析问题。首先创建字符串变量然后传递给方法。
string x = (string)value;
public void Method([FromBody] string id){}