我看过以下内容:
我的终点:
[HttpPost]
[Route("/getter/validatecookie")]
public async Task<IActionResult> GetRankings([FromBody] string cookie)
{
int world = 5;
ApiGetter getter = new ApiGetter(_config, cookie);
if (!await IsValidCookie(getter, world))
{
return BadRequest("Invalid CotG Session");
}
HttpContext.Session.SetString("cotgCookie", cookie);
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
我的请求:
$http.post(ENDPOINTS["Validate Cookie"], cookie , {'Content-Type': 'application/json'});
Run Code Online (Sandbox Code Playgroud)
cookie
我从用户输入发送的字符串在哪里.
请求使用适当的数据发布到端点.但是,我的字符串始终为null.我已经尝试删除[FromBody]
标记,并=
在发布的数据前添加一个没有运气.我还尝试使用上述所有组合添加和删除不同的内容类型.
我正在做这个具体行动的原因很长,对这个问题无关紧要.
无论我做什么,为什么我的参数总是为null?
编辑:我也尝试过使用 {cookie: cookie}
Edit2:请求:
Request URL:http://localhost:54093/getter/validatecookie
Request Method:POST
Status Code:400 Bad Request
Remote Address:[::1]:54093
Run Code Online (Sandbox Code Playgroud)
响应标题
Content-Type:text/plain; …
Run Code Online (Sandbox Code Playgroud) 我是MVC核心新手.
我创建了一个MVC核心项目,它有一个控制器.此控制器具有Get和Post操作方法.如果我使用查询字符串将数据传递给Get方法,它可以正常工作,但是当我将复杂的JSON传递给post方法时,它总是显示为null.
我在做什么:
发布请求
URL: http://localhost:1001/api/users
Content-Type: application/json
Body:
{
"Name":"UserName",
"Gender":"Gender of the user",
"PhoneNumber":"PhoneNumber of the user"
}
Run Code Online (Sandbox Code Playgroud)
这是Post动作方法
[HttpPost]
[Route("api/users")]
public async Task<IActionResult> Post([FromBody]User newUser)
{
...
}
Run Code Online (Sandbox Code Playgroud)
调用post请求时,newUser总是显示null.如果我删除[FromBody]属性,然后我收到newUser对象,但其所有字段都为空.
请帮助我并指导我解决这个问题.
EDITED
这是我的User类
public class User{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string PhoneNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我按照说明进行同这里的JSON数据,但仍收到空.
model-view-controller asp.net-mvc asp.net-web-api2 .net-core asp.net-core