无法将复杂对象传递给另一个Action方法

时间:2018-02-07 10:10:18

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

我正在尝试使用TempData将对象传递给另一个操作方法。控制器不是重定向到操作方法,而是在URL中为我提供了一个白色屏幕,其中包含当前的操作方法。如果我注释掉将对象传递给TempData的行,它会正确地重定向。我的对象太复杂了吗?是否有另一种方法将复杂对象传递给另一个动作方法?

我将对象传递给TempData:

public async Task<IActionResult> UploadFile(IFormFile file)
{
    if (file == null || file.Length == 0)
        return Content("file not selected");
    else
    {                
        var path = Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot",
                "processes.json");

        using (var stream = new FileStream(path, FileMode.Create))
        {
            await file.CopyToAsync(stream);

        }
        RetrieveModels rm = rm = new RetrieveModels(path);
        List<FoundPattern> foundList = new List<FoundPattern>();               
        List<ProcessModel> processes = rm.Processes;
        FindPatterns findp = new FindPatterns(processes, pt.KpiPatterns);
        foundList = findp.fp;
        TempData["list"] = foundList.ToList();
        TempData["Name"] = "Multiple Business Processes";
        return RedirectToAction("Overview");
    }           
}

动作方法我想重定向到:

public IActionResult Overview()
{
    var list = TempData["list"] as List<FoundPattern>;
    ViewData["Name"] = TempData["Name"];
    return View(list);
}

我试图传递给TempData的对象列表:

public class FoundPattern
{
    public KpiPattern pattern = new KpiPattern();
    public List<FoundElement> elements = new List<FoundElement>();
}

对象内的对象列表:

public class FoundElement
{
    public List<string> ElementNames = new List<string>();
    public bool Present { get; set; }
}

工作时的响应标头:

HTTP/1.1 302 Found Location: /Home/Overview Server: Kestrel Set-Cookie: .AspNetCore.Mvc.CookieTempDataProvider=CfDJ8Mu_qDgU_59HncGqqkEm39LG_cUi_rzUyuXXaEYreUFPL2etHRuHPv_5GVKDLcIIcvFhQg1KOzDBfhbvDbjZDpcp8JYzq5kpLHtfnw962pyXNHyCNbx_MOkKwGFSG_dQ_M7LlSVxWYHjqalsSe26K4IlRfGN8V9B5MAgIhWoURgF; path=/; samesite=strict; httponly X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcWkFUXHNvdXJjZVxyZXBvc1xLUEl0b29sXEtQSXRvb2xcSG9tZVxVcGxvYWRGaWxl?= X-Powered-By: ASP.NET Date: Wed, 07 Feb 2018 10:38:40 GMT Content-Length: 0

HTTP/1.1 200 OK Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 Server: Kestrel Set-Cookie: .AspNetCore.Mvc.CookieTempDataProvider=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; samesite=strict X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcWkFUXHNvdXJjZVxyZXBvc1xLUEl0b29sXEtQSXRvb2xcSG9tZVxPdmVydmlldw==?= X-Powered-By: ASP.NET Date: Wed, 07 Feb 2018 10:38:40 GMT

不起作用时的响应标头:

HTTP/1.1 500 Internal Server Error Server: Kestrel X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcWkFUXHNvdXJjZVxyZXBvc1xLUEl0b29sXEtQSXRvb2xcSG9tZVxVcGxvYWRGaWxl?= X-Powered-By: ASP.NET Date: Wed, 07 Feb 2018 10:41:28 GMT Content-Length: 0

1 个答案:

答案 0 :(得分:2)

您正在使用CookieTempDataProvider来管理TempData。唉,顾名思义,它会将TempData存储在cookies中。

问题是您的数据too large适合Cookie。您可能希望使用different ITempDataProvider实施(例如SessionStateTempDataProvider)。