如何将适量的数据传递到Web API应用程序?

克莱·香农

我在Web API控制器中获得了以下代码:

[Route("{unit}/{begindate}/{enddate}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate, 
    [FromBody] string stringifiedjsondata)
{
    List<ProduceUsageSPResults> _produceUsageList = JsonConvert.DeserializeObject<List<ProduceUsageSPResults>>(stringifiedjsondata);
    string _unit = unit;
    string _begindate = String.Format("{0}01", HyphenizeYYYYMM(begindate));
    string _enddate = GetEndOfMonthFor(enddate);
    string appDataFolder =  
HttpContext.Current.Server.MapPath("~/App_Data/");
    string htmlStr = ConvertProduceUsageListToHtml(_produceUsageList);
    string htmlFilename = string.Format("produceUsage_{0}_{1}_{2}.html", 
_unit, _begindate, _enddate); 
    string fullPath = Path.Combine(appDataFolder, htmlFilename);
    File.WriteAllText(fullPath, htmlStr);
}

当通过“ [FromBody]” arg从客户端(Winforms应用程序)仅传递少量(人为)数据时,它可以正常工作,如下所示:

private async Task SaveProduceUsageFileOnServer(string beginMonth, string beginYear, string endMonth, string endYear, DataTable _dtUsage)
{
    string beginRange = String.Format("{0}{1}", beginYear, beginMonth);
    string endRange = String.Format("{0}{1}", endYear, endMonth);
    HttpClient client = new HttpClient {BaseAddress = new Uri("http://localhost:42176")};
    string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT  42#\",\"PackagesMonth1\": 1467}]";
    //string dataAsJson = JsonConvert.SerializeObject(_dtUsage);
    String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}", _unit, beginRange, endRange);
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("", dataAsJson)
    });
    HttpResponseMessage response = await client.PostAsync(uriToCall, content);
}

但是,如果我反转对dataAsJson的赋值的注释,以便它发送实际数据,如下所示:

//string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT  42#\",\"PackagesMonth1\": 1467}]";
string dataAsJson = JsonConvert.SerializeObject(_dtUsage);

...它失败; 没有错误信息。使用JsonConvert.SerializeObject()方法将DataTable(_dtUsage)序列化为json就可以了;只是有大量数据(几千条记录)。永远不会到达客户端对PostAsync()的调用,因此永远不会到达服务器的Post方法。

有一种解决方法可以使它在发送不只是少量数据的情况下成功实现吗?我真的不喜欢通过网络传递这么多的数据(当前客户端和服务器都在本地运行,但是考虑一下部署后的情况),但是另一个选择是从两个客户端执行相同的存储过程(以并从服务器生成Excel电子表格文件)(将数据转换为HTML)。这似乎是那些“ Catch-22”情况中的一种(“如果有则被扣押,如果没有则被隔离”)。

更新

测试出user3093073的想法,我将其添加到Web.config的system.webServer部分:

<security>
  <requestFiltering>
    <requestLimits>
      <headerLimits>
        <add header="Content-type" sizeLimit="10000000" />
      </headerLimits>
    </requestLimits>
  </requestFiltering>
</security>

(基于我在这里找到的内容,但仍然无法正常工作...

更新2

更接近用户user3093073的答案,我也尝试过此操作:

<requestFiltering>
   <requestLimits maxAllowedContentLength="10000000">
     <!--<headerLimits>
       <add header="Content-type" sizeLimit="100" />
     </headerLimits>-->
   </requestLimits>
</requestFiltering>

...也无济于事

更新3

请注意,我将上面的代码放在站点中的每个Web.config文件中,即:

The Web.config file below the \[ProjectName]\Views folder
The Web.config file below the \[ProjectName] folder
The two Web.config files below the \[ProjectName]\Web.config file, namely "Web.Debug.config" and "Web.Release.config"

...或者是查看其位置的另一种方法:

\PlatypusReports\Web.config
\PlatypusReports\Web.config\Web.Debug.config
\PlatypusReports\Web.config\Web.Release.config
\PlatypusReports\Views\Webconfig

更新4

在离开了几天之后又回到原来的位置上,对我而言,现在看来,我一直试图传递的“真实”数据与有效的“伪造/测试”数据不同。

测试数据是一个简单的KeyValuePair的集合。但是,实际数据更为复杂。因此,当我尝试将复杂数据转换为简单的KeyValuePair时,肯定会遇到麻烦。所以代替这个:

new KeyValuePair<string, string>("", dataAsJson)

...我需要这样的东西:

new List<DeliveryPerformanceClass>("", dataAsJson)

...但是那也不起作用;我遇到一些编译错误,例如,“ 'System.Collections.Generic.List'不包含带有2个参数的构造函数'

如果我删除第一个arg以便:

new List<DeliveryPerformanceClass>(dataAsJson)

...我收到其他编译错误,例如“参数1:无法从'string'转换为'int'

大卫·派恩(David Pine)

我相信,由于您已经指定了<requestFiltering>设置,因此还需要设置maxRequestLength正如@brewsky提到的那样,默认情况下只有4MB。检查您的web.config。此设置为2GB

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="2147483648" />
    </system.web>
</configuration>

这样的答案似乎确实解决了类似的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将特性作为应用程序数据传递给Actix Web?

来自分类Dev

如何将数据从Windows应用程序传递到Web应用程序?

来自分类Dev

Web应用程序的安全性-将凭据传递到Web服务API

来自分类Dev

如何将数据传递到控制台应用程序并处理其输出?

来自分类Dev

从链接启动时如何将数据传递给我的应用程序?

来自分类Dev

如何从Android应用程序将敏感数据传递到Google Cloud Endpoints?

来自分类Dev

如何将NSString数据从我的应用程序传递到iOS本机应用程序

来自分类Dev

Ruby:如何将数据传递到后台运行的程序

来自分类Dev

如何将获取的API数据传递到HTML表

来自分类Dev

RAILS:如何将JSON API数据传递到控制器中的哈希

来自分类Dev

如何将代理凭据传递到不支持代理凭据的应用程序上?

来自分类Dev

球衣2:如何将参数从web.xml传递到应用程序?

来自分类Dev

如何将类对象列表从Web服务传递到Windows窗体应用程序

来自分类Dev

如何将数据从Xamarin.Forms应用程序发送到Web API

来自分类Dev

如何将数据从网页传递到要在iOS和Android上安装的应用程序

来自分类Dev

如何将 id 从 Flask 应用程序传递到 mysql 数据库

来自分类Dev

C#应用程序将数据传递到Windows服务

来自分类Dev

从应用程序调用Web API

来自分类Dev

Web应用程序和API

来自分类Dev

对Web应用程序使用Spotify API

来自分类Dev

Web 应用程序中的 Restful API

来自分类Dev

将Valence API用作应用程序

来自分类Dev

单击表的单元格时,如何将数据传递到引导程序模式内的textarea?

来自分类Dev

Azure-安全API应用程序,因此仅逻辑应用程序和Web应用程序可访问

来自分类Dev

如何将Web API 2 Web应用程序分发给我的客户?

来自分类Dev

当我的应用程序通过我的 UITabBar 完成启动时,如何将加载的数据传递给 UITableView?

来自分类Dev

将.net核心Web API的二进制数据传输到TypeScript应用程序

来自分类Dev

如何将数据从Windows(C#)传递到unity3d(C#,android)应用程序?

来自分类Dev

Web 应用程序、Web API 和移动应用程序之间的身份验证

Related 相关文章

  1. 1

    如何将特性作为应用程序数据传递给Actix Web?

  2. 2

    如何将数据从Windows应用程序传递到Web应用程序?

  3. 3

    Web应用程序的安全性-将凭据传递到Web服务API

  4. 4

    如何将数据传递到控制台应用程序并处理其输出?

  5. 5

    从链接启动时如何将数据传递给我的应用程序?

  6. 6

    如何从Android应用程序将敏感数据传递到Google Cloud Endpoints?

  7. 7

    如何将NSString数据从我的应用程序传递到iOS本机应用程序

  8. 8

    Ruby:如何将数据传递到后台运行的程序

  9. 9

    如何将获取的API数据传递到HTML表

  10. 10

    RAILS:如何将JSON API数据传递到控制器中的哈希

  11. 11

    如何将代理凭据传递到不支持代理凭据的应用程序上?

  12. 12

    球衣2:如何将参数从web.xml传递到应用程序?

  13. 13

    如何将类对象列表从Web服务传递到Windows窗体应用程序

  14. 14

    如何将数据从Xamarin.Forms应用程序发送到Web API

  15. 15

    如何将数据从网页传递到要在iOS和Android上安装的应用程序

  16. 16

    如何将 id 从 Flask 应用程序传递到 mysql 数据库

  17. 17

    C#应用程序将数据传递到Windows服务

  18. 18

    从应用程序调用Web API

  19. 19

    Web应用程序和API

  20. 20

    对Web应用程序使用Spotify API

  21. 21

    Web 应用程序中的 Restful API

  22. 22

    将Valence API用作应用程序

  23. 23

    单击表的单元格时,如何将数据传递到引导程序模式内的textarea?

  24. 24

    Azure-安全API应用程序,因此仅逻辑应用程序和Web应用程序可访问

  25. 25

    如何将Web API 2 Web应用程序分发给我的客户?

  26. 26

    当我的应用程序通过我的 UITabBar 完成启动时,如何将加载的数据传递给 UITableView?

  27. 27

    将.net核心Web API的二进制数据传输到TypeScript应用程序

  28. 28

    如何将数据从Windows(C#)传递到unity3d(C#,android)应用程序?

  29. 29

    Web 应用程序、Web API 和移动应用程序之间的身份验证

热门标签

归档