首页 > 解决方案 > 当数据表被大量元素填充时,IIS 崩溃(浏览器返回断点错误)

问题描述

我正在构建一个 MVC asp.net 应用程序,它接收一个 .txt 文件并返回每个单词的出现次数。在我使用更大的文件之前,一切都很好(它打破了,4-5 mb)。我已经在 web.config 中完成了所有必要的设置。

我可以在浏览器上看到它正在填充数据表,但有一次它只是返回错误。我正在将数据从控制器发送到以 IEnumerable 形式查看,它实际上是一个对象列表(word、wordCount)。有人可以解释我该如何解决吗?

接受 txt 文件并返回包含模型的视图的控制器方法:

    [HttpPost]
    public ActionResult Calculate(HttpPostedFileBase file)
    {
       

        bool result = false;
        StringBuilder strbuild = new StringBuilder();
        try
        {
            if (file.ContentLength == 0)
                throw new Exception("Zero length file!");
            else
            {
                var fileName = Path.GetFileName(file.FileName);
                var filePath = Path.Combine(Server.MapPath("~/Document"), fileName);
                file.SaveAs(filePath);
                if (!string.IsNullOrEmpty(filePath))
                {
                    result = true;
                    using (StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~/Document"), fileName)))
                    {
                        while (sr.Peek() >= 0)
                        {
                            strbuild.AppendFormat(sr.ReadLine());
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.StackTrace);
        }

        String s1 = strbuild.ToString();
        ArrayList list = new ArrayList(s1.Split(' ', ',', ':', ';', '!', '?', '.', '"', '(', ')', ']', '[', '}', '{', '\''));
        IDictionary<string, int> dict = new Dictionary<string, int>();
        
       foreach(String l in list)
        {
            if (l.Length > 1)
            {

                if (dict.ContainsKey(l.ToLower()))
                {
                    dict[l.ToLower()] = dict[l.ToLower()] + 1;
                }
                else
                {
                    dict.Add(l.ToLower(), 1);
                }
            }
        }
        if (dict.ContainsKey("")) dict.Remove("");

        dict=dict.OrderBy(key => key.Value).ToDictionary(key=>key.Key, key=>key.Value);

        List<WordModel> words = new List<WordModel>();
        
        

        for (int i=0; i<dict.Count(); i++)
        {
            //System.Diagnostics.Debug.WriteLine("Rec: {0}, Broj Ponavljanja: {1}", dict.ElementAt(i).Key, dict.ElementAt(i).Value);
            WordModel word = new WordModel(dict.ElementAt(i).Key,dict.ElementAt(i).Value);
            words.Add(word);
            
          

        }
        words.Sort();
        words.Reverse();
        
        

        
        
        //ViewData["dict"] = wordsArray;
        return View(words.AsEnumerable());
    }

查看崩溃(返回 status_beakpoint 错误):

     @using WordCounter.Models;



    @model IEnumerable<WordCounter.Models.WordModel>
 @{
  ViewBag.Title = "Word Frequency Calculator";

  //Layout = null;
  //var dict = (IDictionary<string, int>)ViewData["dict"];
  // dict = dict.OrderByDescending(key => key.Value).ToDictionary(key => 
 key.Key, key => key.Value);
  }





      <h2>Calculate</h2>








         @using (Html.BeginForm("Calculate",
                    "Home",
                    FormMethod.Post,
                    new { enctype = "multipart/form-data" }))
      {
     <label for="file">Upload file:</label>
      <input type="file" name="file" id="file" />
       <br>
       <br>

       <input type="submit" value="Upload File" />

        <br>
     <br>

     @ViewBag.Message
}



    <table class="table" style="table-layout:fixed"id="datatable">
      <thead>
        <tr>
          <th>
            Word
        </th>
        <th>
            Word Count
        </th>

       </tr>
   </thead>
   <tbody>
      @foreach (var item in Model)
      {

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.word)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.wordCount)
            </td>
        </tr>

    }
  </tbody>

</table>

   @section scripts{
   <link rel="stylesheet" type="text/css" 
 href="https://cdn.datatables.net/v/dt/dt-1.10.22/af- 
 2.3.5/datatables.min.css" />

   <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt- 
 1.10.22/af-2.3.5/datatables.min.js"></script>
    <script>
    $(document).ready(function () {
        $("#datatable").DataTable({
             " processing": true,
             " serverSide": true,
            "pageLength": 10,
            "AutoWidth": false,
            "LengthChange": false,
            //pokusaj popravke problema sa velikim podacima...
        }  );
    })
 </script>


   }

标签: c#asp.net-mvc

解决方案


您正在运行什么版本的 IIS?IIS 6 中的默认最大请求大小为 4 MB。

当您说它中断时,错误到底是什么意思?

如果您使用的是 IIS 6,请打开 IIS 管理器,导航到您的站点,然后打开请求筛选。在右侧,点击编辑功能设置。在“设置”窗口的“请求限制”区域中,将允许的最大内容长度增加到您需要的长度。

琐碎但值得一提的是,您可以向 MVC 操作添加一个属性,以提高该类型请求的大小限制,即

[HttpPost]
[RequestSizeLimit(100_000_000)]
public IActionResult Foo...

推荐阅读