Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

c# - Cannot pass complex object to another Action method

I'm trying to pass an object using TempData to another action method. Instead of redirecting to the action method, the controller gives me a white screen with the current action method in the URL. If I comment out the line where I pass the object to TempData, it redirects correctly. Is my object too complex to pass? Is there an alternative way of passing a complex object to another action methods?

Where I pass the object to 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");
    }           
}

Action method I want to get redirected to:

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

List of objects I'm trying to pass to TempData:

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

List of objects within object:

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

Response headers when it works:

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

Response headers when it doesn't work:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using the CookieTempDataProvider to manage TempData. Alas, it results in storing the TempData in cookies, as the name suggests.

The problem is that your data is too large to fit in the cookie. You may wish to use a different ITempDataProvider implementation (such as SessionStateTempDataProvider).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...