我正在尝试在Visual Studio中编写非常简单的多平台应用程序(iOS和Android(。这个程序使用网络服务,上传到我的虚拟主机。
这是调用WebAPI(get&post(的代码:
async void post_async(object sender, System.EventArgs e)
{
Console.WriteLine("POST");
try
{
HttpClient httpClient = new HttpClient();
var BaseAddress = "https://mywebsite";
var response = await httpClient.PostAsync(BaseAddress, null);
var message = await response.Content.ReadAsStringAsync();
Console.WriteLine($"RESPONSE: " + message);
}
catch (Exception er)
{
Console.WriteLine($"ERROR: " + er.ToString());
}
}
async void get_async(object sender, System.EventArgs e)
{
try
{
HttpClient httpClient = new HttpClient();
var BaseAddress = "https://mywebsite";
var response = await httpClient.GetAsync(BaseAddress);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine($"RESPONSE: " + content);
}
}
catch (Exception er)
{
Console.WriteLine($"ERROR: " + er.ToString());
}
}
这是 Web API 的非常简单的代码:
[HttpGet]
public ActionResult<string> Get()
{
return "get method ok";
}
[HttpPost]
public ActionResult<string> Post()
{
return "post method ok";
}
非常奇怪的问题,因为对于每个空白,我总是获得"获取方法确定"。所以"get"是可以的,但我不明白为什么我不能调用 post 方法。 我尝试使用邮递员:同样的问题。
我正在使用这个非常简单的代码:
[ActionName("getmet")]
public ActionResult<string> getmet()
{
return "get method ok";
}
[ActionName("postmet")]
public ActionResult<string> postmet()
{
return "post method ok";
}
现在我当然可以打电话给 https://mywebsite/getmet 或postmet,它使用邮递员工作。
如果我使用 [HttpPost] 作为 postmet 方法,在 Postman 上我会得到"404 未找到"。为什么?
var BaseAddress = "https://mywebsite"; //
URL命中率通过difualt获取方法,这意味着与您实际的发布方法URL一样https://mywebsite/Get https://mywebsite/Post
不要调用其他方法,请使用如下所示的代码。
[HttpPost]
[HttpGet]
public ActionResult<string> Get()
{
return "method ok";
}
或者您可以使用 API 路由
或
[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]