我正在尝试将JSON发布到控制器。我在POST的ajax调用中遇到错误。构建成功。因此,我尝试在交互式环境中运行Controller动作,但出现以下错误:
(1,2): error CS0246: The type or namespace name 'HttpPostAttribute' could not be found (are you missing a using directive or an assembly reference?)
(1,2): error CS0246: The type or namespace name 'HttpPost' could not be found (are you missing a using directive or an assembly reference?)
(2,10): error CS0246: The type or namespace name 'ValidateAntiForgeryTokenAttribute' could not be found (are you missing a using directive or an assembly reference?)
(3,16): error CS0246: The type or namespace name 'GitJSON' could not be found (are you missing a using directive or an assembly reference?)
(3,61): error CS0246: The type or namespace name 'GitJSON' could not be found (are you missing a using directive or an assembly reference?)
+ additional 1 error
我正在使用ASP.NET Core Web应用程序进行项目。 这是我要发布的JSON:
[{“ AvatarURL”:“ https://avatars1.githubusercontent.com/u/7849225?v=4”,“名称”:“ simplenlg”,“分数”:22.82041,“ Updatedat”:“ 2018-07-21T10:58:33Z”},{“ AvatarURL“:” https://avatars1.githubusercontent.com/u/8931462?v=4“,”名称“:” aws-microservices-deploy-options“,”分数“:20.521696,” Updatedat“:” 2018-07-20T12:22:07Z“},{” AvatarURL“:” https://avatars3.githubusercontent.com/u/4046529?v=4“,” Name“:” useful-jenkins-groovy-init-scripts“,”分数“:21.447626,” Updatedat“:”“ 2018-07-18T19:52:02Z”}, {“ AvatarURL”:“ https://avatars1.githubusercontent.com/u/6755615?v=4”,“名称”:“ dnsjava”,“分数”:34.74705,“ Updatedat”:“” 2018-06-28T15:16:45Z“},{” AvatarURL“:” https://avatars3.githubusercontent.com/u/504773?v=4“,”名称“:” luke“,”分数“:19.239859,” Updatedat“:” 2018-06-28T07:27:26Z“},{” AvatarURL“:” https://avatars3.githubusercontent.com/u/536912?v=4“ ,“名称”:“ Wicket-tutorial-examples”,“得分”:37.265644,“ Updatedat”:“ 2018-07-14T04:28:50Z”},{“ AvatarURL”:“ https://avatars0.githubusercontent.com/u/33330803?v=4”,“名称“:” nexus-repository-apt“,”得分“:44.401646,” Updatedat“:” 2018-07-06T18:28:13Z“},{” AvatarURL“:” https://avatars2.githubusercontent.com/u/1321963?v=4“,”名称“ :“ marc4j”,“得分”:28.282797,“ Updatedat”:“ 2018-07-07T15:58:57Z”},{“ AvatarURL”:“ https://avatars0.githubusercontent.com/u/5622390?v=4”,“名称”:“ jamonapi”,“得分”:24.564436,“ Updatedat”:“ 2018-07-16T07:44:35Z”},{“ AvatarURL”:“ https://avatars2.githubusercontent.com/u/1123352?v=4”,“名称”:“ osgi.enroute”,“得分”:7.6444883 ,“ Updatedat”:“” 2018-07-17T08:2 6:51Z“}]
我收到400错误的请求错误:
无法加载资源:服务器响应状态为400 (错误请求)
这是控制器:
namespace Github.Controllers
{
public class Gitcontroller : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public GitJSON /*async Task<ActionResult>*/ Updateto(GitJSON gitjson)
{
return gitjson;
}
}
这是模型:
namespace Github.Models
{
public class gitd
{
public int ID { get; set; }
public string AvatarURL { get; set; }
public string Name { get; set; }
public decimal Score { get; set; }
public DateTime Updatedat { get; set; }
}
public class GitJSON
{
public List<gitd> gitdList { set; get; }
}
}
我正在使用的命名空间是:
using Microsoft.AspNetCore.Mvc;
using Github.Models;
ajax调用网址:
需要帮助:)。
答案 0 :(得分:2)
请在方法参数之前添加[FromBody],例如
[HttpPost]
[ValidateAntiForgeryToken]
public GitJSON /*async Task<ActionResult>*/ Updateto([FromBody]GitJSON gitjson)
{
return gitjson;
}
答案 1 :(得分:1)
似乎您在这里遇到了一些问题,但是至少其中一个步骤是编辑您发布的模型或JSON。
根据您的GitJSON
模型定义,模型绑定需要采用以下格式的JSON数据:
{
gitdList: [
{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225? v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},
{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}
]
}
此外,您需要向操作参数添加[FromBody]
属性,以指定要从请求正文获取数据。 MVC doesn't do this by default。