gpt4 book ai didi

c# - 后续 http 请求后 TempData 是否仍然可用?

转载 作者:行者123 更新时间:2023-11-30 18:11:38 24 4
gpt4 key购买 nike

我的教科书上说“TempData gets immediately destroyed after it is used in after it used in after it used in after it used in after it used in subsequent HTTP request”,所以我写了一个简单的测试来验证

下面是我的代码:

// SimpleForm.cshtml is just a simple view that uses a form to send post request to ReceiveForm action method
//Result.cshtml is just a simple view that products an output

public class HomeController : Controller
{
public ViewResult Index() => View("SimpleForm");

[HttpPost]
public RedirectToActionResult ReceiveForm(string name, string city)
{
TempData["name"] = name;
TempData["city"] = city;
return RedirectToAction(nameof(Transfer));
}

public RedirectToActionResult Transfer()
{
string name = TempData["name"] as string;
string city = TempData["city"] as string;
return RedirectToAction(nameof(Data));
}

public ViewResult Data()
{
string name = TempData["name"] as string;
string city = TempData["city"] as string;
return View("Result", $"{name} lives in {city}");
}
}

所以当应用程序运行时,它首先进入 Index() 操作方法,我用名称和城市填写表单并按下提交按钮,然后它进入 ReceiveForm() 操作方法,设置 TempData 并重定向到 Transfer() 操作方法。

在 Transfer() 操作方法中,我读取了 TempData,因此根据教科书,TempData 应该被销毁并且无法在下一个 http 请求中读取。

但是在Data()中,我发现还是可以读到TempData,看下面的截图:

enter image description here

我检查了chrome dev tool,有一个post请求和两个get请求,都是正确的。 那么 TempData 什么时候真正被销毁?

附加代码:

SimpleForm.cshtml:

@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Controllers and Actions</title>
<link rel="stylesheet" asp-href-include="lib/bootstrap/dist/css/*.min.css" />
</head>
<body class="m-1 p-1">
<form method="post" asp-action="ReceiveForm">
<div class="form-group">
<label for="name">Name:</label>
<input class="form-control" name="name" />
</div>
<div class="form-group">
<label for="name">City:</label>
<input class="form-control" name="city" />
</div>
<button class="btn btn-primary center-block" type="submit">Submit</button>
</form>
</body>
</html>

结果.cshtml:

@model string
@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Controllers and Actions</title>
<link rel="stylesheet" asp-href-include="lib/bootstrap/dist/css/*.min.css" />
</head>
<body class="m-1 p-1">
Model Data: @Model
</body>
</html>

最佳答案

对于您的场景,这是由 RedirectToActionResult 引起的。对于 RedirectToActionResult,即 IKeepTempDataResult

public class RedirectToActionResult : ActionResult, IKeepTempDataResult

SaveTempDataFilter是保存临时数据的过滤器。它将调用 SaveTempData

private static void SaveTempData(
IActionResult result,
ITempDataDictionaryFactory factory,
IList<IFilterMetadata> filters,
HttpContext httpContext)
{
var tempData = factory.GetTempData(httpContext);

for (var i = 0; i < filters.Count; i++)
{
if (filters[i] is ISaveTempDataCallback callback)
{
callback.OnTempDataSaving(tempData);
}
}

if (result is IKeepTempDataResult)
{
tempData.Keep();
}

tempData.Save();
}

对于SaveTempData,它会检查IActionResult result是否为IKeepTempDataResult。如果是,它将保留临时数据。

如果你想避免在请求之间保留 tempData,你可以将 RedirectToAction 更改为 LocalRedirect,如

public IActionResult Transfer()
{
string name = TempData["name"] as string;
string city = TempData["city"] as string;
return LocalRedirect("~/Home/Data");
//return RedirectToAction(nameof(Data));
}

关于c# - 后续 http 请求后 TempData 是否仍然可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com