C# 如何将多部分表单数据列表发布到asp.net web api操作

C# 如何将多部分表单数据列表发布到asp.net web api操作,c#,asp.net,asp.net-mvc,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Asp.net Web Api,我正在使用asp.NETWebAPI。我试图保存一个复杂类的列表,这些类的属性类型为图像数据或文本数据。我有两门课 问题类 public class Question { public int QuestionId { get; set; } public string Text { get; set; } public string Type { get; set; } } public class Response { public int ResponseI

我正在使用asp.NETWebAPI。我试图保存一个复杂类的列表,这些类的属性类型为图像数据或文本数据。我有两门课

问题类

public  class Question
{
   public int QuestionId { get; set; }
   public string Text { get; set; }
   public string Type { get; set; }
}
public  class Response
{
    public int ResponseId { get; set; }
    public int QuestionId { get; set; }
    public int UserId { get; set; }
    public string ResponseText { get; set; }
}
响应类

public  class Question
{
   public int QuestionId { get; set; }
   public string Text { get; set; }
   public string Type { get; set; }
}
public  class Response
{
    public int ResponseId { get; set; }
    public int QuestionId { get; set; }
    public int UserId { get; set; }
    public string ResponseText { get; set; }
}
这是申请表的样子

--------------------------------------
1, Question Text (Label)
   Response (TextBox Control)
--------------------------------------
2, Question Text (Label)
   Response (File upload control)
--------------------------------------
3, Question Text (Label)
   Response (TextArea)
-------------------------------------
4, Question Text (Label)
   Response (Either File upload,Textbox other control)
-------------------------------------
       | Submit Answers Button |
-------------------------------------
当用户单击SubmitAnswers按钮时,所有页面数据都将发布到WebAPI操作

如果所有都是简单的文本框,即简单的表单数据,那么我将数据(列表)发布到web api,就像json中的一样

[{"QuestionId":1,"UserId":321,"ResponseText":"Hello"},{"QuestionId":2,"UserId":321,"ResponseText":"Great"},{"QuestionId":3,"UserId":321,"ResponseText":"Simple"}]
在web api中

public HttpResponseMessage SubmitAnswers(HttpRequestMessage request,List<Response>    responses)
{
....
....
....
}
公共HttpResponseMessage提交者(HttpRequestMessage请求,列表响应)
{
....
....
....
}

但在我的例子中,我有多部分表单数据,而且我必须发布多个表单数据,即每个问题和回答都被视为一个表单,因此我们如何将多部分表单数据列表发布到asp.net web api操作。请指导我。

您是否考虑过使用
MultipartFormDataProvider
当请求是具有“formurlencoded”数据+文件的多部分请求时?…但是如果您的请求类似于“json”数据+文件,那么您可以使用
MultipartMemoryStreamProvider
…如果文件很大且内存使用率令人担忧,您可以创建一个自定义的
MultipartStreamProvider

这是一个尚未测试的代码,但它应该能让您了解如何开始

// Create a MultipartFormDataContent object
var multipartContent = new MultipartFormDataContent();

int counter = 0;
foreach (var question in questions)
{
    // Serialize each "question" item found in "questions"
    var questionJson = JsonConvert.SerializeObject(question, 
    new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    });

    // Add each serialized question to the multipart object
    multipartContent.Add(new StringContent(questionJson, Encoding.UTF8, "application/json"), "question" + counter);
    counter++;
}

// Post a petition with a  multipart/form-data body
var response = new HttpClient()
    .PostAsync("http://multiformserver.com:53908/api/send", multipartContent)
    .Result;

var responseContent = response.Content.ReadAsStringAsync().Result;

我建议你自己看看请愿书的内容和回复。你可以在网站上找到关于如何使用Fiddler的更多信息。

为什么你要为此使用web api而不是MVC控制器操作,这会简单得多。我需要通过iphone应用程序的web api提供后端支持,他们的应用程序有问题表单,他们将数据发布到web api。