【问题标题】:HttpPostAttribute could not be found. -Asp.net core找不到 HttpPostAttribute。 -Asp.net 核心
【发布时间】:2018-07-21 19:58:07
【问题描述】:

我正在尝试将 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","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL" :"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":" https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":" https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name" :"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples" ,"Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score" :44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":" 2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44: 35Z"},{"头像URL":"https://avatars2.githubusercontent.com/u/1123352?v=4","姓名":" osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}]

我收到 400 bad request 错误:

加载资源失败:服务器响应状态为 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 调用 URL:

网址:'http://localhost:60294/Git/Updateto',

需要帮助:)。

【问题讨论】:

  • 您的表单是否包含防伪令牌?
  • 你说build是绿色的,但是CS0246 error是编译错误。请澄清。
  • 嗨,我刚开始学习 ASP.NET 已经一个星期了所以不太了解,我得到了:1>----- 构建开始:项目:Github,配置:调试任何CPU ------ 因此我假设构建成功。
  • 已移除,[ValidateAntiForgeryToken],编译报错,但是ajax中正在执行succes函数。

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

请在方法参数之前添加 [FromBody],例如

   [HttpPost]
   [ValidateAntiForgeryToken]
   public GitJSON /*async Task<ActionResult>*/ Updateto([FromBody]GitJSON gitjson)
            {
                return gitjson;
            }

【讨论】:

  • 尝试添加 [FormBody] - 得到以下错误:(3,62): error CS0246: The type or namespace name 'FromBodyAttribute' could not be found (are you missing a using directive or an assembly reference?)
【解决方案2】:

看起来您在这里遇到的问题很少,但至少其中一个步骤是编辑您发布的模型或 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.

【讨论】:

  • 尝试添加 [FormBody] - 得到以下错误:(3,62): error CS0246: The type or namespace name 'FromBodyAttribute' could not be found (are you missing a using directive or an assembly reference?)
  • 非常感谢 gitdList 指针,我不会注意到它,我该如何命名 JSON 数组,对不起,初学者:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多