Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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# WebApi-响应状态代码不表示成功:404(未找到)_C#_Json_Rest_Asp.net Web Api2_Bson - Fatal编程技术网

C# WebApi-响应状态代码不表示成功:404(未找到)

C# WebApi-响应状态代码不表示成功:404(未找到),c#,json,rest,asp.net-web-api2,bson,C#,Json,Rest,Asp.net Web Api2,Bson,我试图通过Newtonsoft.Json.Bson将图像及其元数据(如名称)从C#console客户端发送到WebApi Restful服务器 客户端代码: public async Task SendRequestAsync(byte[] imageBytes, string fileName) { using (var stream = new MemoryStream()) using (var bson = new Newtonsoft.J

我试图通过
Newtonsoft.Json.Bson
将图像及其元数据(如名称)从C#console客户端发送到WebApi Restful服务器

客户端代码:

    public async Task SendRequestAsync(byte[] imageBytes, string fileName)
    {
        using (var stream = new MemoryStream())
        using (var bson = new Newtonsoft.Json.Bson.BsonWriter(stream))
        {
            var jsonSerializer = new JsonSerializer();

            var json = JObject.FromObject(new
            {
                name = fileName,
                content = imageBytes
            });

            jsonSerializer.Serialize(bson, json);

            var client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:1920")
            };

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/bson"));

            var byteArrayContent = new ByteArrayContent(stream.ToArray());
            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");

            var result = await client.PostAsync(
                    "api/Files", byteArrayContent);
            try
            {
                HttpResponseMessage response = result.EnsureSuccessStatusCode();

                Console.Out.WriteLine(response.IsSuccessStatusCode);
                Console.Out.WriteLine(response.Headers);
                Console.Out.WriteLine(response.Content);
                Console.Out.WriteLine(response.StatusCode);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e);
            }
        }
    }
服务器代码

// WebApi Controller
    public JsonResult<object> Post([FromBody]FileModel fileModel)
    {
        Console.Out.WriteLine(fileModel.name);
        Console.Out.WriteLine(fileModel.length);

        return Json<object>(new
        {
            status = HttpStatusCode.OK,
            length = fileModel.length,
            name = fileModel.name
        });
    }

// Model Class
    public class FileModel
    { 
        public string name { get; set; }
        public byte[] content { get; set; }
        public int length { get; set; }
    }
//WebApi控制器
公共JsonResult帖子([FromBody]文件模型文件模型)
{
Console.Out.WriteLine(fileModel.name);
Console.Out.WriteLine(fileModel.length);
返回Json(新的
{
状态=HttpStatusCode.OK,
length=fileModel.length,
name=fileModel.name
});
}
//模范班
公共类文件模型
{ 
公共字符串名称{get;set;}
公共字节[]内容{get;set;}
公共整数长度{get;set;}
}
如果我发送了一个28KB的映像,那么服务器将成功接收该映像。但如果我尝试发送20MB的图像,则会出现以下错误

System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at FileUploadConsole.Program.<SendRequestAsync>d__1.MoveNext() in c:\users\Projects\FileUploadConsole\FileUploadConsole\Program.cs:line 58
System.Net.Http.HttpRequestException:响应状态代码不表示成功:404(未找到)。
在System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()中
在c:\users\Projects\FileUploadConsole\FileUploadConsole\Program.cs中的FileUploadConsole.Program.d\u 1.MoveNext()处:第58行
第58行是
httpresponsemessageresponse=result.EnsureSuccessStatusCode()


为什么大型映像找不到服务?

请将此信息放入您的web.config:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="4294967295" /> 
    </requestFiltering>
  </security>

maxAllowedContentLength
以字节为单位指定最大内容长度,并用uint表示,请参阅。
我的示例中的值设置为(4 gb)。

它可能取决于web服务器配置。我的意思是最大请求大小。在服务器端,我将
maxRequestLength
设置为>145MB-
,但错误保持不变。我试图上传27MB的图片。