I am using TempData for temp storage of a list item.
I have used below code in program.cs
builder.Services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
builder.Services.AddSession();
//Other middleware
app.UseSession();
On a get request i have created a view model and stored the data from viewmodel into Tempdata as given below
TempData["vMClient"] = JsonConvert.SerializeObject(vMClient);
return View(vMClient);
Now whenever i am using Tempdata.Peek method my browser is throwing error code 431
View Page Code.
@model VMClient
@{
var a = @TempData.Peek("vMClient");
}
Kindly help me to resolve this issue.
2
Answers
Seems like the
HTTP Error 431
is caused by passing thevMClient
parameter to the view: it exceeded header size. And you don’t need to do that, because this data is already passing by theTempData
. What you need is to useDeserializeObject()
.Try the following. In the action method:
In the view:
You can use HTTP cookies or session state as storage mechanism for
TempData
. The cookie-basedTempData
provider is the default. Session state can handle larger amounts of data compared to cookies state.The HTTP 431 status code indicates that the server is unwilling to process the request because its header fields are too large. This usually happens when there are too many cookies or large cookies being sent in the request.
Based on the following example from docs you can enable the session-based TempData provider, by calling
AddSessionStateTempDataProvider
extension method.Another way, you can also store data in
Session
instead ofTempData
.Refer to: How to Set and get Session values