无法将 NameValueCollection 传递给 POST ASP.NET/C# 中的 WebAPI 5.0 核心
Unable to pass NameValueCollection to POST WebAPI 5.0 core in ASP.NET / C#
我正在尝试使用 HttpPost
将 NameValueCollection
传递给 ASP.NET Core 5.0 Web API 控制器 (WebserviceController.cs
)。我正在尝试 post 2 个名称值对作为示例:key1,value1
和 key2,value2
C# (.NET Framework 4.8) 客户端代码为:
webClient WebClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
String response = Encoding.ASCII.GetString(webClient.UploadValues("https://localhost:44312/webservice", new NameValueCollection() { { "key1", "value1" }, { "key2", "value2" } }));
邮递员截图:
我在 C# 客户端中遇到错误:The remote server returned an error: (415) Unsupported Media Type.
在 Postman 中:
我尝试了以下两种方法来接收 NameValueCollection
到 ASP.NET Core 5.0 Web API 控制器使用 HttpPost
:
namespace Webservice.Controllers
{
[ApiController]
[Route("[controller]")]
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public String Post([FromBody] NameValueCollection Values)
{
return "values received";
}
}
}
并且:
public class classNameValueCollection
{
public NameValueCollection SampleValues;
}
[ApiController]
[Route("[controller]")]
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public String Post([FromBody] classNameValueCollection Values)
{
return "values received";
}
}
在第一个方法中,我直接尝试在 Post
方法中获取 NameValueCollection
,而在第二种方法中,我创建了一个 class,它有一个 NameValueCollection
变量并使用 class 对象在 Post
方法中接收 NameValueCollection
。
但我得到了同样的错误。我在这里做错了什么?
代替NameValueCollection
和FromBody
,使用IFormCollection
和FromForm
Reference Image
已通过邮递员测试。
namespace Webservice.Controllers
{
[ApiController]
[Route("[controller]")]
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public String Post([FromForm] Microsoft.AspNetCore.Http.IFormCollection Values)
{
string val1 = Values["key1"].ToString();
string val2 = Values["key2"].ToString();
return "values received";
}
}
}
你在参数中使用了错误的类型,正确的方法是
public String Post([FromForm] classNameValueCollection Values)
我正在尝试使用 HttpPost
将 NameValueCollection
传递给 ASP.NET Core 5.0 Web API 控制器 (WebserviceController.cs
)。我正在尝试 post 2 个名称值对作为示例:key1,value1
和 key2,value2
C# (.NET Framework 4.8) 客户端代码为:
webClient WebClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
String response = Encoding.ASCII.GetString(webClient.UploadValues("https://localhost:44312/webservice", new NameValueCollection() { { "key1", "value1" }, { "key2", "value2" } }));
邮递员截图:
我在 C# 客户端中遇到错误:The remote server returned an error: (415) Unsupported Media Type.
在 Postman 中:
我尝试了以下两种方法来接收 NameValueCollection
到 ASP.NET Core 5.0 Web API 控制器使用 HttpPost
:
namespace Webservice.Controllers
{
[ApiController]
[Route("[controller]")]
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public String Post([FromBody] NameValueCollection Values)
{
return "values received";
}
}
}
并且:
public class classNameValueCollection
{
public NameValueCollection SampleValues;
}
[ApiController]
[Route("[controller]")]
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public String Post([FromBody] classNameValueCollection Values)
{
return "values received";
}
}
在第一个方法中,我直接尝试在 Post
方法中获取 NameValueCollection
,而在第二种方法中,我创建了一个 class,它有一个 NameValueCollection
变量并使用 class 对象在 Post
方法中接收 NameValueCollection
。
但我得到了同样的错误。我在这里做错了什么?
代替NameValueCollection
和FromBody
,使用IFormCollection
和FromForm
Reference Image
已通过邮递员测试。
namespace Webservice.Controllers
{
[ApiController]
[Route("[controller]")]
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public String Post([FromForm] Microsoft.AspNetCore.Http.IFormCollection Values)
{
string val1 = Values["key1"].ToString();
string val2 = Values["key2"].ToString();
return "values received";
}
}
}
你在参数中使用了错误的类型,正确的方法是
public String Post([FromForm] classNameValueCollection Values)