ViewStateException Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents the exception that is thrown when the view state cannot be loaded or validated. This class cannot be inherited.
public ref class ViewStateException sealed : Exception
[System.Serializable]
public sealed class ViewStateException : Exception
[<System.Serializable>]
type ViewStateException = class
inherit Exception
interface ISerializable
Public NotInheritable Class ViewStateException
Inherits Exception
- Inheritance
- Attributes
- Implements
The following code example demonstrates how to implement a method that deserializes a base64-encoded string and returns an ICollection collection of property settings. The Deserialize method can throw an HttpException exception that contains a ViewStateException object as an inner exception. The example shows how you can catch an HttpException exception and retrieve the properties from the ViewStateException object.
private ICollection LoadControlProperties(string serializedProperties)
{
ICollection controlProperties = null;
// Create an ObjectStateFormatter to deserialize the properties.
ObjectStateFormatter formatter = new ObjectStateFormatter();
try
{
// Call the Deserialize method.
controlProperties = (ArrayList)formatter.Deserialize(serializedProperties);
}
catch (HttpException e)
{
ViewStateException vse = (ViewStateException)e.InnerException;
String logMessage;
logMessage = "ViewStateException. Path: " + vse.Path + Environment.NewLine;
logMessage += "PersistedState: " + vse.PersistedState + Environment.NewLine;
logMessage += "Referer: " + vse.Referer + Environment.NewLine;
logMessage += "UserAgent: " + vse.UserAgent + Environment.NewLine;
LogEvent(logMessage);
if (vse.IsConnected)
{
HttpContext.Current.Response.Redirect("ErrorPage.aspx");
}
else
{
throw e;
}
}
return controlProperties;
}
Private Function LoadControlProperties(ByVal serializedProperties As String) As ICollection
Dim controlProperties As ICollection = Nothing
' Create an ObjectStateFormatter to deserialize the properties.
Dim formatter As New ObjectStateFormatter()
Try
' Call the Deserialize method.
controlProperties = CType(formatter.Deserialize(serializedProperties), ArrayList)
Catch e As HttpException
Dim vse As ViewStateException
Dim logMessage As String
vse = e.InnerException
logMessage = "ViewStateException. Path: " + vse.Path + Environment.NewLine
logMessage += "PersistedState: " + vse.PersistedState + Environment.NewLine
logMessage += "Referer: " + vse.Referer + Environment.NewLine
logMessage += "UserAgent: " + vse.UserAgent + Environment.NewLine
LogEvent(logMessage)
If (vse.IsConnected) Then
HttpContext.Current.Response.Redirect("ErrorPage.aspx")
Else
Throw e
End If
End Try
Return controlProperties
End Function 'LoadControlProperties
If an exception occurs when the page loads the view-state information, the page creates a ViewStateException object and passes it as an inner exception within an HttpException exception. The HttpException exception is re-thrown to be handled by the application.
The ViewStateException class stores information about the exception in the Message property. If the request has permission to access the ServerVariables property, then the Message property contains the values returned from the RemoteAddress, RemotePort, UserAgent, PersistedState, Referer, and Path properties; otherwise, the Message property contains a generic view-state error message.
A view-state exception can be the result of a Message Authentication Codes (MAC) validation error. If the page attribute enableViewStateMac
is set to true
, view-state information is encoded with a MAC identifier. When the view-state information is posted back to the server, the page verifies the encoding to ensure that it has not been altered by the user. If the page cannot validate the MAC encoding on the view-state information, it will throw an HttpException exception with the ViewStateException object as an inner exception.
For MAC validation to succeed, the view-state information must be hashed by the same key when it is sent and received. Within a Web farm, the machine key on each server should be set to a common key.
View |
Initializes a new instance of the ViewStateException class. |
Data |
Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception) |
Help |
Gets or sets a link to the help file associated with this exception. (Inherited from Exception) |
HResult |
Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception) |
Inner |
Gets the Exception instance that caused the current exception. (Inherited from Exception) |
Is |
Gets a value indicating whether the client is currently connected to the server. |
Message |
Gets debugging information about the HTTP request that resulted in a view-state exception. |
Path |
Gets the path of the HTTP request that resulted in a view-state exception. |
Persisted |
Gets the contents of the view-state string that, when read, caused the view-state exception. |
Referer |
Gets the URL of the page that linked to the page where the view-state exception occurred. |
Remote |
Gets the IP address of the HTTP request that resulted in a view-state exception. |
Remote |
Gets the port number of the HTTP request that resulted in a view-state exception. |
Source |
Gets or sets the name of the application or the object that causes the error. (Inherited from Exception) |
Stack |
Gets a string representation of the immediate frames on the call stack. (Inherited from Exception) |
Target |
Gets the method that throws the current exception. (Inherited from Exception) |
User |
Gets the browser type of the HTTP request that resulted in a view-state exception. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Initializes a new instance of the ViewStateException class with serialized data. |
Get |
Gets the runtime type of the current instance. (Inherited from Exception) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
To |
Creates and returns a string representation of the current exception. (Inherited from Exception) |
Serialize |
Obsolete.
Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception) |
Produto | Versões |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Comentários do .NET
O .NET é um projeto código aberto. Selecione um link para fornecer comentários: