C# 如何在.NETC中的同一url RESTAPI中使用POST和GET方法
我有一个简单的存储过程,它获取用户名和密码,选择所有信息并返回 我这里有这个请求代码:C# 如何在.NETC中的同一url RESTAPI中使用POST和GET方法,c#,asp.net,rest,api,C#,Asp.net,Rest,Api,我有一个简单的存储过程,它获取用户名和密码,选择所有信息并返回 我这里有这个请求代码: [AcceptVerbs("GET", "POST")] public Student GetInfoByLogin(Student studentm) { //Response response = new Response(); Student student = new Student(); try {
[AcceptVerbs("GET", "POST")]
public Student GetInfoByLogin(Student studentm)
{
//Response response = new Response();
Student student = new Student();
try
{
Connection();
SqlCommand cmd = new SqlCommand("GetInfoByUserNameAndPassword", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", studentm.UserName);
cmd.Parameters.AddWithValue("@Password", studentm.Password);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
student.StundentID = Convert.ToInt32(reader["StudentID"]);
student.UserName = reader["UserName"].ToString();
student.Password = reader["Password"].ToString();
student.PhoneNumber = reader["PhoneNumber"].ToString();
student.Name = reader["Name"].ToString();
student.Age = Convert.ToInt32(reader["Age"]);
student.InstitutionName = reader["InstitutionName"].ToString();
student.RechargedAmount = Convert.ToInt32(reader["RechargedAmount"]);
student.IsPending = Convert.ToInt32(reader["IsPending"]);
}
conn.Close();
}
catch (Exception ex)
{
//
}
return student;
}
我测试了这个请求,它在Postman中运行良好。但是当我点击url时,没有任何信息。我认为这是ExecuteReader操作。它应该展示一些东西!!!有如下错误日志:
<Message>The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.</Message>
没有MediaTypeFormatter可用于从媒体类型为“application/octet stream”的内容中读取类型为“Student”的对象
但在《邮递员》中效果很好。您能告诉我如何发送POST并使用GET在单个url调用中获取返回值吗
听起来你想用两个步骤来完成一个一步的过程 您不使用HTTP请求发布然后获取,而是发布然后获取响应。此响应可能是传递给另一个端点的信息,但所有HTTP请求都会得到某种形式的响应,其中可能包含数据
[AcceptVerbs("POST")]
public Student GetInfoByLogin(Student studentm)
{
return GetStudentData();
}
错误本身表明您正在错误地调用端点
请求包含实体正文,但不包含内容类型标头。此资源不支持推断的媒体类型“应用程序/八位字节流”。
这意味着您的端点本身没有问题,是调用方没有处理它
考虑一下这种联系
您可能在读或写端都犯了错误,因为您没有指出错误来自调用方或处理程序通常从查询字符串获取参数,从请求正文发布。student参数是一个对象,无法从查询字符串中读取。您是否有一个如何调用该方法的示例?错误消息似乎很明确,您应该提供一个内容类型头application/xml或application/json,具体取决于您序列化参数的方式,这意味着我在endpoint中没有任何问题。我必须通过电话来处理吗?我可以调用此url但不能调用GetAsync吗?或者我必须用PostAsync调用它;我不知道!我试图获取一些信息,但发送了一些参数。有办法吗?GET only GET thing POST only send参数。我无法生成url或数据库表以获得结果。@MahirMuzahid我认为您应该阅读更多关于http协议的内容,因为您似乎不了解什么是请求/响应