C# Owin自主机不一致中的多部分Web API操作
我有一个自托管的owin web api应用程序,它有一个操作,将多部分/表单数据保存到服务器中的内容目录中(稍后将以http链接的形式发送到用户的图像文件,这是一个好方法吗?)。以下是控制器代码:C# Owin自主机不一致中的多部分Web API操作,c#,asp.net-web-api,mono,multipartform-data,owin,C#,Asp.net Web Api,Mono,Multipartform Data,Owin,我有一个自托管的owin web api应用程序,它有一个操作,将多部分/表单数据保存到服务器中的内容目录中(稍后将以http链接的形式发送到用户的图像文件,这是一个好方法吗?)。以下是控制器代码: [HttpPost] public async Task<object> Post() { if (!Request.Content.IsMimeMultipartContent("form-data")) {
[HttpPost]
public async Task<object> Post()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
string rootFolder = Path.Combine("Content");
var provider = new NamedMultipartFormDataStreamProvider(rootFolder);
try
{
//read the form data
await Request.Content.ReadAsMultipartAsync(provider);
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Saving to server... " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
Mono JIT编译器版本3.8.0中失败的POST
Remote Address:127.0.0.1:2004
Request URL:http://localhost:2004/api/stories
Request Method:POST
Status Code:415 Unsupported Media Type
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:681954
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryW4NqTv782jlZ186F
Host:localhost:2004
Origin:http://localhost:2004
Referer:http://localhost:2004/api/help
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Request Payload
------WebKitFormBoundaryW4NqTv782jlZ186F
Content-Disposition: form-data; name="caption"
This is Test Image Caption
------WebKitFormBoundaryW4NqTv782jlZ186F
Content-Disposition: form-data; name="image1"; filename="1407231597673.png"
Content-Type: image/png
------WebKitFormBoundaryW4NqTv782jlZ186F--
Response Headersview source
Content-Length:0
Date:Mon, 08 Sep 2014 23:39:11 GMT
Keep-Alive:timeout=15,max=100
Server:Mono-HTTPAPI/1.0
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
我该如何解决这个问题
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
问候
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
编辑
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
如果我对ISIMEMultipartContent(“表单数据”)部分进行注释,它会给出follow错误
ExceptionMessage: "Invalid 'HttpContent' instance provided.
It does not have a content-type header value.
'HttpContent' instances must have a content-type header starting with 'multipart/'. Parameter name: content",
ExceptionType: "System.ArgumentException",
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
如果有人想知道是否使用了NamedMultipartFormDataStreamProvider
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
编辑2:
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
似乎是Mono bug,它包含在下面的if中
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
这可能是Mono错误,其中if Request.Content.Headers.ContentType从未初始化,并且始终为null。通过调试时,同时在Windows中正确初始化Request.Content.Headers.ContentType,但在Mono中从未正确初始化
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}
if (Request.Content.Headers.ContentType == null)
{
Console.WriteLine("header is null");
}