Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 内部服务器错误:部署my WebAPI后缓冲区不能为null_C#_Asp.net_Entity Framework_Asp.net Web Api - Fatal编程技术网

C# 内部服务器错误:部署my WebAPI后缓冲区不能为null

C# 内部服务器错误:部署my WebAPI后缓冲区不能为null,c#,asp.net,entity-framework,asp.net-web-api,C#,Asp.net,Entity Framework,Asp.net Web Api,我刚刚将我的ASP.NET应用程序部署到一个托管公司的远程服务器上,当我试图在postman中将我的POST请求发送到我的URL时,我收到一个内部服务器错误,错误消息如下 “消息”:“发生错误。” “ExceptionMessage”:“缓冲区不能为空。参数名称:缓冲区” “ExceptionType”:“System.ArgumentNullException” StackTrace信息在第334行指向我的AccountController 下面是我的AccountController: p

我刚刚将我的ASP.NET应用程序部署到一个托管公司的远程服务器上,当我试图在postman中将我的POST请求发送到我的URL时,我收到一个内部服务器错误,错误消息如下

“消息”:“发生错误。”

“ExceptionMessage”:“缓冲区不能为空。参数名称:缓冲区”

“ExceptionType”:“System.ArgumentNullException”

StackTrace信息在第334行指向我的AccountController

下面是我的AccountController:

 public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        else
        {
            var stream = new MemoryStream(model.ImageArray);//This is the line the StackTrace Points to
            var guid = Guid.NewGuid().ToString();
            var file = string.Format("{0}.jpg", guid);
            var folder = "~\\Content\\UserData";
            var fullpath = string.Format("{0}\\{1}", folder, file);
            var response = Filehelpers.UploadPhoto(stream, folder, file);


            var user = new ApplicationUser()
            {
                UserName = model.Alias,
                Email = model.Email,
                Alias = model.Alias,
                PhoneNumber = model.PhoneNumber,
                DateJoined = model.DateJoined,
                ImagePath = fullpath



            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            return Ok();

PS:当我在测试时,它在本地工作得很好,但当我部署并想要测试时,它开始抛出错误。

该异常告诉我们的是,您试图调用一个带有空参数的方法。因此它是无效的。似乎图像阵列是罪魁祸首

所以我们需要用两种方法来解决这个问题。我们需要在图像数组中添加一个
[Required]
属性,这样以后就不会发生这种情况


我们还需要确保发送到API的请求的格式正确,并且包含图像数组

您收到此错误是因为
model.ImageArray
为空。要证明这一点,您只需在服务器中记录
model.ImageArray
,然后进行检查


我的建议是使用null或空检查保护您的
ImageArray
,如果未在帖子正文中传递,则返回BADDREQUEST。

您正在internet上部署一个存储明文密码的Web应用程序?@Lankymart这不也是一个可能的重复吗?一定有人问过arguementnulleception是什么了。@Jlalonde当然,欢迎你去找一个。可能是@Lankymart的副本,不,我没有以明文形式存储它。这是一个哈希密码。我将尝试使用必需的属性选项,但我确信我的json数据已进行了属性格式化,并且它包含一个图像数组,因为我在数据库仍然位于本地时发送了相同的数据。现在我在线部署了它,错误就出现了。我个人从未尝试过在帖子中发送图像,所以也许可以看看您的json是否真正格式化,newtonsoft是否可以支持这种序列化OK,但我只是在将API部署到在线后才遇到这个问题。我检查并确认newtonsoft支持序列化。我都不知道该怎么想了,很有趣。你是在通过网关吗?准确地记录从HTTP上下文到API的内容,可能会丢弃一个头。让我们将调试分解为几个部分。我发了一封多好的信。2我收到了什么,3我的代码在做什么。老实说,我不清楚你的意思。我所知道的是,我曾经使用Azure进行API部署,它工作得非常完美,但是它太贵了,我决定尝试其他方法,我遇到了所有这些问题。我不明白你所说的网关是什么意思?我知道映像数组返回null,我想找到一种方法来纠正这个问题。我将检查它是否为null,然后看看会发生什么。但我发送的是相同的数据,而数据库是本地的,工作正常。但现在我把它放到了网上,问题开始显现出来假设网站托管公司使用的是IIS服务器,我猜是帖子正文超过了maxrequest长度。默认情况下,它带有2个MEG,但托管服务器可能会覆盖默认值。能否尝试在webconfig maxAllowedContentLength和maxRequestLength中设置以下两个属性。你可以找到更多关于这个的信息。希望这有帮助。
[Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    //custom models

    //represents the username
    public string Alias { get; set; }
    //represents when the user signed up
    public DateTime DateJoined { get; set; }
    //represents users profile picture
    [NotMapped]
    public byte[] ImageArray { get; set; }
    //represents the uri of the users picture
    public string ImagePath { get; set; }
    public string PhoneNumber { get; set; }
}