C# 找不到HttpPostAttribute-Asp.net内核
我正在尝试将JSON发布到控制器。我在对POST的ajax调用中遇到错误。构建成功。因此,我尝试在interactive中运行控制器操作,但出现以下错误:C# 找不到HttpPostAttribute-Asp.net内核,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我正在尝试将JSON发布到控制器。我在对POST的ajax调用中遇到错误。构建成功。因此,我尝试在interactive中运行控制器操作,但出现以下错误: (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: Th
(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核心Web应用程序的项目。
这是我试图发布的JSON:
[{“AvatarURL”:“名称”:“simplenlg”,“分数”:22.82041,“更新日期”:“2018-07-21T10:58:33Z”},{“AvatarURL”:“名称”:“aws微服务部署选项”,“分数”:20.521696,“更新日期”:“2018-07-20T12:22:07Z”},{“AvatarURL”:“名称”:“有用的jenkins groovy初始化脚本”,“分数”:21.447626,“更新日期”:“2018-07-18T19:52:02Z”{“AvatarURL“分数”:34.74705,“更新日期”:“2018-06-28T15:16:45Z”;“阿瓦塔鲁尔”:“姓名”:“卢克”,“分数”:19.239859,“更新日期”:“2018-06-28T07:27:26Z”;“阿瓦塔鲁尔”:“姓名”:“Wicket教程示例”,“分数”:37.265644,“更新日期”:“2018-07-14T04:28:50Z”;“鲁尔”:“姓名”:“nexus存储库”,“分数”:44.401646,“更新日期”:“2018-07-T128:50Z”{“阿瓦塔鲁尔”:“姓名”:“marc4j”,“分数”:28.282797,“更新日期”:“2018-07-07T15:58:57Z”},{“阿瓦塔鲁尔”:“姓名”:“杰莫纳皮”,“分数”:24.564436,“更新日期”:“2018-07-16T07:44:35Z”},{“阿瓦塔鲁尔”:“姓名”:“osgi中途”,“分数”:7.6444883,“更新日期”:“2018-07-17T08:26:51Z”}]
我收到一个400错误请求错误:
加载资源失败:服务器响应状态为400
(请求错误)
这是控制器:
namespace Github.Controllers
{
public class Gitcontroller : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public GitJSON /*async Task<ActionResult>*/ Updateto(GitJSON gitjson)
{
return gitjson;
}
}
ajax调用URL:
url:“”
需要帮助:)。请在方法参数之前添加[FromBody],例如
[HttpPost]
[ValidateAntiForgeryToken]
public GitJSON /*async Task<ActionResult>*/ Updateto([FromBody]GitJSON gitjson)
{
return gitjson;
}
[HttpPost]
[ValidateAntiForgeryToken]
公共GitJSON/*异步任务*/Updateto([FromBody]GitJSON)
{
返回gitson;
}
看起来您在这里没有什么问题,但至少有一个步骤是编辑您发布的模型或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]
attribute to action参数以指定您希望从请求正文获取数据。您的表单是否包含防伪令牌?您说过构建是绿色的,但这是一个编译错误。请澄清。您好,我刚刚开始学习ASP.NET一周了,所以不太了解,我得到:1>----构建开始:项目:Github,Co配置:调试任何CPU------因此我假设构建成功。已删除[ValidateAntiForgeryToken],获取编译错误,但正在ajax中执行succes函数。尝试添加[FormBody]-获取以下错误:(3,62):错误CS0246:找不到类型或命名空间名称“FromBodyAttribute”(是否缺少using指令或程序集引用?
尝试添加[FormBody]-获取以下错误:(3,62):错误CS0246:找不到类型或命名空间名称“FromBodyAttribute”(是否缺少using指令或程序集引用?)
非常感谢gitdList指针,我永远不会注意到它,我如何命名JSON数组,抱歉,初学者:)
[HttpPost]
[ValidateAntiForgeryToken]
public GitJSON /*async Task<ActionResult>*/ Updateto([FromBody]GitJSON gitjson)
{
return gitjson;
}
{
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"}
]
}