简体   繁体   中英

How can I pass large strings from one controller to another?

I'm attempting to send an HTML table from one controller to another, but because of the size of the table I'm getting HTTP 400 errors. I think the errors are coming from a lower level than the framework, as it's just a raw HTTP 400 error instead of an error with a stack, query, etc. like a normal exception does when debugging.

If I limit the size of the DataTable to a single row, it works just fine. But once I get past two or three rows the length of the query string gets long enough it starts throwing errors.

I tried passing the DataTable itself to MailboxRulesResults and then doing the conversion from DataTable to HTML table there using a List<object> , but the DataTable never made it to the function.

Is there a better way to do this or do I need to tweak a config somewhere?

Thanks in advance

        [HttpPost, ValidateAntiForgeryToken]
        public IActionResult GetMailboxRules(MailboxRulesModel mailbox)
        {
            string currentuser = HttpContext.User.FindFirst("preferred_username")?.Value;
            string guid = Guid.NewGuid().ToString();
            using (DataTable results = Mail.GetMailboxRules(mailbox.EmailAddress, guid, currentuser))
            {
                List<string> return_list = new List<string>
                {
                    UtilityFunctions.ConvertDataTableToHTML(results, guid),
                    guid
                };
                return RedirectToAction("MailboxRulesResults", "Mail", new { data = return_list });
            }
        }
        [HttpPost, ValidateAntiForgeryToken]
        public IActionResult MailboxRulesResults(List<string> data)
        {
            ViewBag.TableHTML = data[0];
            ViewBag.Guid = data[1];
            return View();
        }

Ended up changing my original view from

<form asp-controller="Mail" asp-action="GetMailboxRules" method="post"

to

<form asp-controller="Mail" asp-action="MailboxRulesResults" method="post"

and then having the MailboxRulesResults action handle everything.

 [HttpPost, ValidateAntiForgeryToken]
        public IActionResult MailboxRulesResults(MailboxRulesModel mailbox)
        {
            string currentuser = HttpContext.User.FindFirst("preferred_username")?.Value;
            string guid = Guid.NewGuid().ToString();
            List<MailboxRulesModel> rules = Mail.GetMailboxRules(mailbox.EmailAddress, guid, currentuser);
            using (DataTable dt = UtilityFunctions.CreateDataTable(rules))
            {
                string html = UtilityFunctions.ConvertDataTableToHTML(dt, guid);
                ViewBag.TableHTML = html;
                ViewBag.Guid = guid;
                return View();
            }
        }

Just do the following and save table as text file on server than get it on next request.

   private IHostingEnvironment _env;
       var webRoot = _env.WebRootPath;
           var file = System.IO.Path.Combine(webRoot, "table.txt");
 [HttpPost, ValidateAntiForgeryToken]
    public IActionResult GetMailboxRules(MailboxRulesModel mailbox)
    {

        string currentuser = HttpContext.User.FindFirst("preferred_username")?.Value;
        string guid = Guid.NewGuid().ToString();
        using (DataTable results = Mail.GetMailboxRules(mailbox.EmailAddress, guid, currentuser))
        {
            List<string> return_list = new List<string>
            {
                UtilityFunctions.ConvertDataTableToHTML(results, guid),
                guid
            };

          using(TextWriter tw = new StreamWriter(file))
          {
          foreach (string s in return_list)
           tw.WriteLine(s);  
          }
            return {Your action method without parameter}
        }
    }

Then retrieve:

[HttpPost, ValidateAntiForgeryToken]
    public IActionResult MailboxRulesResults(List<string> data)
    {

       using (StreamReader sr = new StreamReader(file))
        {

            String line;
            // Read line by line
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine(line);                

            }
        }
        ViewBag.TableHTML = line;
        ViewBag.Guid = data[1];
        return View();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM