HttpParseException 类

发生分析错误时引发的异常。

**命名空间:**System.Web
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<SerializableAttribute> _
Public NotInheritable Class HttpParseException
    Inherits HttpException
用法
Dim instance As HttpParseException
[SerializableAttribute] 
public sealed class HttpParseException : HttpException
[SerializableAttribute] 
public ref class HttpParseException sealed : public HttpException
/** @attribute SerializableAttribute() */ 
public final class HttpParseException extends HttpException
SerializableAttribute 
public final class HttpParseException extends HttpException

备注

HttpParseException 类是一个特定于 HTTP 的异常类,ASP.NET 可通过该类输出分析器异常信息。有关引发和处理异常的更多信息,请参见 处理和引发异常

示例

下面的示例演示如何使用 HttpParseException 来自定义在页分析期间生成的错误。此示例中定义了一个自定义的 HtmlSelect 控件。如果自定义控件的子元素不属于指定的类型,则在自定义 HtmlSelectBuilderGetChildControlType 重写方法中将引发一个 HttpParseException。若要生成一个分析异常,请将子元素文本 MyCustomOption 更改为其他任何字符串。

<%@ Page Language="VB"%>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpParseException Example</title>
</head>
  <body>
    <form id="Form1" runat="server">
      <h3>HttpParseException Example</h3>

      <aspSample:CustomHtmlSelectWithHttpParseException
       id="customhtmlselect1"
       runat="server">
      <aspSample:MyCustomOption optionid="option1" value="1"/>
      <aspSample:MyCustomOption optionid="option2" value="2"/>
      <aspSample:MyCustomOption optionid="option3" value="3"/>
      </aspSample:CustomHtmlSelectWithHttpParseException>

    </form>

  </body>

</html>
<%@ Page Language="C#"%>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpParseException Example</title>
</head>
  <body>
    <form id="Form1" runat="server">
      <h3>HttpParseException Example</h3>

      <aspSample:CustomHtmlSelectWithHttpParseException
       id="customhtmlselect1"
       runat="server">
      <aspSample:MyCustomOption optionid="option1" value="1"/>
      <aspSample:MyCustomOption optionid="option2" value="2"/>
      <aspSample:MyCustomOption optionid="option3" value="3"/>
      </aspSample:CustomHtmlSelectWithHttpParseException>

    </form>

  </body>

</html>
Imports System
Imports System.Security.Permissions
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Namespace Samples.AspNet.VB
    ' Define a child control for the custom HtmlSelect.
    Public Class MyCustomOption
        Private _id As String
        Private _value As String


        Public Property optionid() As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

        Public Property value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property
    End Class

    ' Define a custom HtmlSelectBuilder.
    Public Class MyHtmlSelectBuilderWithparseException
        Inherits HtmlSelectBuilder

        <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
        Public Overrides Function GetChildControlType(ByVal tagName As String, ByVal attribs As IDictionary) As Type

            ' Distinguish between two possible types of child controls.
            If tagName.ToLower().EndsWith("mycustomoption") Then

                Return GetType(MyCustomOption)

            Else
                
                Throw New HttpParseException("This custom HtmlSelect control" & _ 
                         "requires child elements of the form ""MyCustomOption""")
            End If

        End Function

    End Class


    <ControlBuilderAttribute(GetType(MyHtmlSelectBuilderWithparseException))> _
    Public Class CustomHtmlSelectWithHttpParseException
        Inherits HtmlSelect

        ' Override the AddParsedSubObject method.
        Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

            Dim _outputtext As String
            If TypeOf obj Is MyCustomOption Then
                _outputtext = "custom select option : " + CType(obj, MyCustomOption).value
                Dim li As New ListItem(_outputtext, CType(obj, MyCustomOption).value)
                MyBase.Items.Add(li)
            End If

        End Sub

    End Class

End Namespace
using System;
using System.Security.Permissions;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS
{
    // Define a child control for the custom HtmlSelect.
    public class MyCustomOption
    {
        string _id;
        string _value;

        public string optionid
        {
            get
            { return _id; }
            set
            { _id = value; }
        }

        public string value
        {
            get
            { return _value; }
            set
            { _value = value; }
        }

    }

    // Define a custom HtmlSelectBuilder.
    public class MyHtmlSelectBuilderWithparseException : HtmlSelectBuilder
    {
        [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
        public override Type GetChildControlType(string tagName, IDictionary attribs)
        {
            // Distinguish between two possible types of child controls.
            if (tagName.ToLower().EndsWith("mycustomoption"))
            {
                return typeof(MyCustomOption);
            }
            else
            {
                throw new HttpParseException("This custom HtmlSelect control" +                                                  "requires child elements of the form \"MyCustomOption\"");
            }
        }

    }

    [ControlBuilderAttribute(typeof(MyHtmlSelectBuilderWithparseException))]
    public class CustomHtmlSelectWithHttpParseException : HtmlSelect
    {
        // Override the AddParsedSubObject method.
        protected override void AddParsedSubObject(object obj)
        {
            
            string _outputtext;
            if (obj is MyCustomOption)
            {
                _outputtext = "custom select option : " + ((MyCustomOption)obj).value;
                ListItem li = new ListItem(_outputtext, ((MyCustomOption)obj).value);
                base.Items.Add(li);
            }
        }      
    }

}

.NET Framework 安全性

继承层次结构

System.Object
   System.Exception
     System.SystemException
       System.Runtime.InteropServices.ExternalException
         System.Web.HttpException
          System.Web.HttpParseException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

HttpParseException 成员
System.Web 命名空间
HtmlSelect
HtmlSelectBuilder

其他资源

处理和引发异常