【问题标题】:how to post list of multipart form-data to asp.net web api action如何将多部分表单数据列表发布到 asp.net web api 操作
【发布时间】:2014-01-08 05:59:44
【问题描述】:

我正在研究 asp.net Web API。我正在尝试保存具有图像数据或文本数据类型属性的复杂类的列表。我有两节课,

问题类

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 |
-------------------------------------

当用户点击提交答案按钮时,所有页面数据将被发布到 web api 操作。

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

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

在网络 API 中,

public HttpResponseMessage SubmitAnswers(HttpRequestMessage request,List<Response>    responses)
{
....
....
....
}

但在我的情况下,我有多部分表单数据,而且我必须发布多个表单数据,即每个带有响应的问题都被视为一个表单,所以我们如何将多部分表单数据列表发布到 asp。网络 web api 操作。请指导我。

【问题讨论】:

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

标签: c# asp.net asp.net-mvc asp.net-web-api


【解决方案1】:

您是否考虑过使用MultipartFormDataProvider,其中请求是具有“formurlencoded”数据+文件的多部分请求?...但如果您的请求类似于“json”数据+文件,那么您可以使用MultipartMemoryStreamProvider。 ..如果文件很大并且内存使用是一个问题,您可以创建一个自定义MultipartStreamProvider...

【讨论】:

    【解决方案2】:

    这是尚未测试的代码,但它应该为您提供一个开始的想法。

    // 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 自己查看请愿书的内容和回复。您可以在Fiddler's Documentation 网站上找到有关如何使用 Fiddler 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-18
      • 2017-09-20
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2017-03-30
      相关资源
      最近更新 更多