【发布时间】:2018-08-01 01:57:09
【问题描述】:
我想使用 httpclient 发布数据或使用 Web api。我对 asp.net 的了解是有限的,所以任何指向我正确方向的人都会有所帮助。 我有一个 url,我给它起了一个通用名称 localhost。我需要从这个 url 发布或使用服务。
这是我的代码示例:这是名为 Student 的模型类。
namespace Student.Models
{
public class StudentInfo
{
public string id{ get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string subject { get; set; }
}
}
这是名为 StudentController 的代码控制器:
public void Post([FromBody] string id, string firstname, string lastname, string subject)
{
Student stu = new Student();
stu.id = id;
stu.firstname= firstname;
stu.lastname = lastname;
stu.subject = subject;
var client = new HttpClient { BaseAddress = new Uri("https://localhost") };
// call sync
var response = client.PostAsync("/api/student/exist",
content).Result;
if (response.IsSuccessStatusCode)
{
}
}
我在这一行得到错误内容不存在于当前上下文中:
// call sync
var response = client.PostAsync("/api/membership/exist", content).Result;
【问题讨论】: