We are experiencing a problem where our viewstate seems to be getting truncated, possibly by a proxy server or firewall at a client site. The error we get is:
Invalid length for a Base-64 char array.
We updated the web.config with the following setting in the <pages> element:
maxPageStateFieldLength="1000"
this seemed to mostly resolve the issue but we now intermittently get this exception instead:
Invalid viewstate: Missing field: __VIEWSTATE47
Has anyone successfully used the maxPageStateFieldLength property without getting the missing field error? I have seen other solutions that handle the viewstate chunking in code rather than using maxPageStateFieldLength and we are wondering if we should
do that instead?
Yes absolutely, Firewalls can truncate viewstate if the field length is
too long. The work around isn't necessarily to limit the viewstate
(although I would recommend making this as small as possible something
like datagrid sorting / paging doesn't work so well with it turned off
:) ), instead you break the viewstate up into several fields. This is
actually pretty simple.
Just add these overrides to your base page class...
#region Modified ViewState Code
protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter format = new LosFormatter();
int cnt = 0;
try
{
Cnt =
Convert.ToInt32(Request["__VIEWSTATE0"].ToString());
}
catch (System.NullReferenceException)
{}
catch (System.FormatException)
{}
System.Text.StringBuilder s = new
System.Text.StringBuilder();
for ( int i = 1; i <= cnt; i++ )
try
{
s.Append( Request["__VIEWSTATE" +
i.ToString()].ToString() );
}
catch (System.NullReferenceException)
{}
try
{
object o = format.Deserialize(s.ToString());
if (o.GetType() == typeof(Triplet))
return o;
else if (o.GetType() == typeof(string) && (o as
string).Length == 0)
o = null;
return o;
}
catch (System.Exception)
{
}
}
protected override void SavePageStateToPersistenceMedium(object
viewState)
{
LosFormatter format = new LosFormatter();
System.IO.StringWriter writer = new System.IO.StringWriter();
format.Serialize(writer, viewState);
System.Text.StringBuilder s = new
System.Text.StringBuilder(writer.ToString());
int cnt = 1;
int left = s.Length;
while( left > 0 )
{
int cut= (left > 1000) ? 1000 : left; //Change 1000 to
whatever size you need
RegisterHiddenField("__VIEWSTATE" + cnt.ToString(),
s.ToString().Substring(0,cut));
S = s.Remove(0,cut);
left -= cut;
cnt++;
}
cnt--;
This is what the maxPageStateFieldLength config attribute is supposed to do but then we get the missing field errors.
We have since worked around the problem by implementing our own PageAdapter and PageStatePersister classes so that we can persist viewstate to the database. This has actually improved the performance of our application noticably as there is less info being
sent across the wire between the server and browser (especially important as we are using ajax with lots of small postbacks that still send the entire viewstate).
Jason Hill
Contributor
2019 Points
494 Posts
ViewState getting truncated
May 29, 2007 06:42 AM|LINK
Hi,
We are experiencing a problem where our viewstate seems to be getting truncated, possibly by a proxy server or firewall at a client site. The error we get is:
Invalid length for a Base-64 char array.
We updated the web.config with the following setting in the <pages> element:
maxPageStateFieldLength="1000"
this seemed to mostly resolve the issue but we now intermittently get this exception instead:
Invalid viewstate: Missing field: __VIEWSTATE47
Has anyone successfully used the maxPageStateFieldLength property without getting the missing field error? I have seen other solutions that handle the viewstate chunking in code rather than using maxPageStateFieldLength and we are wondering if we should do that instead?
Cheers,
Jason
Rex Lin - MSFT
All-Star
17422 Points
2116 Posts
Re: ViewState getting truncated
May 31, 2007 03:04 AM|LINK
HI, Jason Hill :
Yes absolutely, Firewalls can truncate viewstate if the field length is
too long. The work around isn't necessarily to limit the viewstate
(although I would recommend making this as small as possible something
like datagrid sorting / paging doesn't work so well with it turned off
:) ), instead you break the viewstate up into several fields. This is
actually pretty simple.
Just add these overrides to your base page class...
#region Modified ViewState Code
protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter format = new LosFormatter();
int cnt = 0;
try
{
Cnt =
Convert.ToInt32(Request["__VIEWSTATE0"].ToString());
}
catch (System.NullReferenceException)
{}
catch (System.FormatException)
{}
System.Text.StringBuilder s = new
System.Text.StringBuilder();
for ( int i = 1; i <= cnt; i++ )
try
{
s.Append( Request["__VIEWSTATE" +
i.ToString()].ToString() );
}
catch (System.NullReferenceException)
{}
try
{
object o = format.Deserialize(s.ToString());
if (o.GetType() == typeof(Triplet))
return o;
else if (o.GetType() == typeof(string) && (o as
string).Length == 0)
o = null;
return o;
}
catch (System.Exception)
{
}
}
protected override void SavePageStateToPersistenceMedium(object
viewState)
{
LosFormatter format = new LosFormatter();
System.IO.StringWriter writer = new System.IO.StringWriter();
format.Serialize(writer, viewState);
System.Text.StringBuilder s = new
System.Text.StringBuilder(writer.ToString());
int cnt = 1;
int left = s.Length;
while( left > 0 )
{
int cut= (left > 1000) ? 1000 : left; //Change 1000 to
whatever size you need
RegisterHiddenField("__VIEWSTATE" + cnt.ToString(),
s.ToString().Substring(0,cut));
S = s.Remove(0,cut);
left -= cut;
cnt++;
}
cnt--;
RegisterHiddenField("__VIEWSTATE0", cnt.ToString());
}
#endregion
Good Luck
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support
If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved
Jason Hill
Contributor
2019 Points
494 Posts
Re: ViewState getting truncated
May 31, 2007 03:13 AM|LINK
Thanks Rex,
This is what the maxPageStateFieldLength config attribute is supposed to do but then we get the missing field errors.
We have since worked around the problem by implementing our own PageAdapter and PageStatePersister classes so that we can persist viewstate to the database. This has actually improved the performance of our application noticably as there is less info being sent across the wire between the server and browser (especially important as we are using ajax with lots of small postbacks that still send the entire viewstate).
Jason
vishalsharma...
Member
364 Points
107 Posts
Re: ViewState getting truncated
Jun 01, 2007 01:55 PM|LINK
merydith
Member
2 Points
4 Posts
Re: ViewState getting truncated
Apr 01, 2008 09:08 AM|LINK
Hi Jason, I am facing the same problem too. Do you mind sharing your PageAdapter and PageStatePersister classes?
Thanks in adv
manishr4
Member
2 Points
1 Post
Re: ViewState getting truncated
Jul 15, 2011 12:27 PM|LINK
Hi Rex Lin would u please send/share "LosFormatter" class. This help will be greately appreciated. Thanks!