HttpException Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Décrit une exception qui s’est produite pendant le traitement des requêtes HTTP.
public ref class HttpException : System::Runtime::InteropServices::ExternalException
public class HttpException : System.Runtime.InteropServices.ExternalException
[System.Serializable]
public class HttpException : System.Runtime.InteropServices.ExternalException
type HttpException = class
inherit ExternalException
[<System.Serializable>]
type HttpException = class
inherit ExternalException
Public Class HttpException
Inherits ExternalException
- Héritage
- Dérivé
- Attributs
Exemples
L’exemple de code suivant montre comment utiliser la HttpException classe pour déclencher des exceptions personnalisées. La Button_Click méthode détermine quel bouton a été cliqué, puis appelle la ou CheckBoolean la CheckNumber méthode. Si la valeur entrée par l’utilisateur dans la zone de texte ne correspond pas au type attendu, une HttpException exception est créée et levée avec un message personnalisé en fonction du type d’erreur.
Important
Cet exemple contient une zone de texte qui accepte l’entrée utilisateur, qui est une menace de sécurité potentielle. Par défaut, ASP.NET pages web valident que l’entrée utilisateur n’inclut pas de script ou d’éléments HTML. Pour plus d’informations, consultez Vue d’ensemble des exploits de script.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CheckNumber()
{
try
{
// Check whether the value is an integer.
String convertInt = textbox1.Text;
Convert.ToInt32(convertInt);
}
catch (Exception e)
{
// Throw an HttpException with customized message.
throw new HttpException("not an integer");
}
}
void CheckBoolean()
{
try
{
// Check whether the value is an boolean.
String convertBool = textbox1.Text;
Convert.ToBoolean(convertBool);
}
catch (Exception e)
{
// Throw an HttpException with customized message.
throw new HttpException("not a boolean");
}
}
void Button_Click(Object sender, EventArgs e)
{
try
{
// Check to see which button was clicked.
Button b = (Button)sender;
if (b.ID.StartsWith("button1"))
CheckNumber();
else if (b.ID.StartsWith("button2"))
CheckBoolean();
label1.Text = "You entered: " + textbox1.Text;
label1.ForeColor = System.Drawing.Color.Black;
}
// Catch the HttpException.
catch (HttpException exp)
{
label1.Text = "An HttpException was raised. "
+ "The value entered in the textbox is " + exp.Message.ToString();
label1.ForeColor = System.Drawing.Color.Red;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpException Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<b>Enter a value in the text box.</b>
<br />
<asp:TextBox ID="textbox1"
Runat="server">
</asp:TextBox>
<br />
<asp:Button ID="button1"
Text="Check for integer."
OnClick="Button_Click"
Runat="server">
</asp:Button>
<br />
<asp:Button ID="button2"
Text="Check for boolean."
OnClick="Button_Click"
Runat="server">
</asp:Button>
<br />
<asp:Label ID="label1"
Runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CheckNumber()
Try
' Check whether the value is an integer.
Dim convertInt As String = textbox1.Text
Convert.ToInt32(convertInt)
Catch e As Exception
' Throw an HttpException with customized message.
Throw New HttpException("not an integer")
End Try
End Sub
Sub CheckBoolean()
Try
' Check whether the value is an boolean.
Dim convertBool As String = textbox1.Text
Convert.ToBoolean(convertBool)
Catch e As Exception
' Throw an HttpException with customized message.
Throw New HttpException("not a boolean")
End Try
End Sub
Sub Button_Click(ByVal sender As [Object], ByVal e As EventArgs)
Try
' Check to see which button was clicked.
Dim b As Button = CType(sender, Button)
If b.ID.StartsWith("button1") Then
CheckNumber()
ElseIf b.ID.StartsWith("button2") Then
CheckBoolean()
End If
label1.Text = "You entered: " + textbox1.Text
label1.ForeColor = System.Drawing.Color.Black
' Catch the HttpException.
Catch exp As HttpException
label1.Text = "An HttpException was raised. " + "The value entered in the textbox is " + exp.Message.ToString()
label1.ForeColor = System.Drawing.Color.Red
End Try
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpException Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<b>Enter a value in the text box.</b>
<br />
<asp:TextBox ID="textbox1"
Runat="server">
</asp:TextBox>
<br />
<asp:Button ID="button1"
Text="Check for integer."
OnClick="Button_Click"
Runat="server">
</asp:Button>
<br />
<asp:Button ID="button2"
Text="Check for boolean."
OnClick="Button_Click"
Runat="server">
</asp:Button>
<br />
<asp:Label ID="label1"
Runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
Remarques
La classe HttpException est une classe d’exception spécifique à HTTP qui permet ASP.NET de générer des informations d’exception. Pour plus d’informations sur la levée et la gestion des exceptions, consultez Exceptions.
Constructeurs
| Nom | Description |
|---|---|
| HttpException() |
Initialise une nouvelle instance de la HttpException classe et crée un objet vide HttpException . |
| HttpException(Int32, String, Exception) |
Initialise une nouvelle instance de la classe à l’aide HttpException d’un code d’état de réponse HTTP, d’un message d’erreur et de la InnerException propriété. |
| HttpException(Int32, String, Int32) |
Initialise une nouvelle instance de la classe à l’aide HttpException d’un code d’état de réponse HTTP, d’un message d’erreur et d’un code d’exception. |
| HttpException(Int32, String) |
Initialise une nouvelle instance de la classe à l’aide HttpException d’un code d’état de réponse HTTP et d’un message d’erreur. |
| HttpException(SerializationInfo, StreamingContext) |
Initialise une nouvelle instance de la classe HttpException avec des données sérialisées. |
| HttpException(String, Exception) |
Initialise une nouvelle instance de la classe à l’aide HttpException d’un message d’erreur et de la InnerException propriété. |
| HttpException(String, Int32) |
Initialise une nouvelle instance de la classe à l’aide HttpException d’un message d’erreur et d’un code d’exception. |
| HttpException(String) |
Initialise une nouvelle instance de la classe à l’aide HttpException d’un message d’erreur fourni. |
Propriétés
| Nom | Description |
|---|---|
| Data |
Obtient une collection de paires clé/valeur qui fournissent des informations supplémentaires définies par l’utilisateur sur l’exception. (Hérité de Exception) |
| ErrorCode |
Obtient l’erreur |
| HelpLink |
Obtient ou définit un lien vers le fichier d’aide associé à cette exception. (Hérité de Exception) |
| HResult |
Obtient ou définit HRESULT, valeur numérique codée affectée à une exception spécifique. (Hérité de Exception) |
| InnerException |
Obtient l’instance Exception qui a provoqué l’exception actuelle. (Hérité de Exception) |
| Message |
Obtient un message qui décrit l’exception actuelle. (Hérité de Exception) |
| Source |
Obtient ou définit le nom de l’application ou de l’objet qui provoque l’erreur. (Hérité de Exception) |
| StackTrace |
Obtient une représentation sous forme de chaîne des images immédiates sur la pile des appels. (Hérité de Exception) |
| TargetSite |
Obtient la méthode qui lève l’exception actuelle. (Hérité de Exception) |
| WebEventCode |
Obtient les codes d’événement associés à l’exception HTTP. |
Méthodes
| Nom | Description |
|---|---|
| CreateFromLastError(String) |
Crée une exception HttpException basée sur le code d’erreur retourné par la méthode |
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetBaseException() |
En cas de substitution dans une classe dérivée, retourne la Exception qui est la cause racine d’une ou plusieurs exceptions ultérieures. (Hérité de Exception) |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetHtmlErrorMessage() |
Obtient le message d’erreur HTML à retourner au client. |
| GetHttpCode() |
Obtient le code d’état de la réponse HTTP pour revenir au client. |
| GetObjectData(SerializationInfo, StreamingContext) |
Obtient des informations sur l’exception et l’ajoute à l’objet SerializationInfo . |
| GetObjectData(SerializationInfo, StreamingContext) |
En cas de substitution dans une classe dérivée, définit l'SerializationInfo avec des informations sur l’exception. (Hérité de Exception) |
| GetType() |
Obtient le type d’exécution de l’instance actuelle. (Hérité de Exception) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| ToString() |
Retourne une chaîne qui contient le HRESULT de l’erreur. (Hérité de ExternalException) |
Événements
| Nom | Description |
|---|---|
| SerializeObjectState |
Se produit lorsqu’une exception est sérialisée pour créer un objet d’état d’exception qui contient des données sérialisées sur l’exception. (Hérité de Exception) |