我正在尝试将HTML表从一个控制器发送到另一个控制器,但是由于表的大小,我遇到了HTTP 400错误。我认为这些错误来自比框架更低的级别,因为它只是原始的HTTP 400错误,而不是堆栈,查询等错误,就像调试时的正常异常一样。
如果我将DataTable
的大小限制为一行,它就可以正常工作。但是一旦超过两行或三行,查询字符串的长度就足够长了,它将开始引发错误。
我尝试将DataTable
本身传递到MailboxRulesResults
,然后使用List<object>
从那里进行从DataTable到HTML表的转换,但是DataTable从未实现该功能。
是否有更好的方法可以做到这一点?还是需要在某个地方调整配置?
预先感谢
[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();
}
答案 0 :(得分:0)
最终从更改了我的原始视图
<form asp-controller="Mail" asp-action="GetMailboxRules" method="post"
到
<form asp-controller="Mail" asp-action="MailboxRulesResults" method="post"
然后让MailboxRulesResults
操作处理所有事情。
[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();
}
}
答案 1 :(得分:0)
只需执行以下操作,然后将表另存为服务器上的文本文件即可,而不是在下一个请求时获取。
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}
}
}
然后检索:
[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();
}