HttpMethodConstraint クラス

定義

ASP.NET ルーティングが URL とルートの一致を判定するときに使用できる HTTP 動詞を定義できるようにします。

public ref class HttpMethodConstraint : System::Web::Routing::IRouteConstraint
public class HttpMethodConstraint : System.Web.Routing.IRouteConstraint
type HttpMethodConstraint = class
    interface IRouteConstraint
Public Class HttpMethodConstraint
Implements IRouteConstraint
継承
HttpMethodConstraint
実装

次の例は、 Route という名前httpMethodのキーを持ちConstraints、 クラスのインスタンスである値を持つ項目をプロパティに含む オブジェクトをHttpMethodConstraint示しています。

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    string[] allowedMethods = { "GET", "POST" };
    HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods);

    Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
    reportRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } };

    routes.Add(reportRoute);
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim urlPattern As String
    Dim reportRoute As Route
    Dim allowedMethods() As String = {"GET", "POST"}
    Dim methodConstraints As HttpMethodConstraint  
    
    methodConstraints = New HttpMethodConstraint(allowedMethods)
    
    Dim constraintValues = New With {.httpMethod = methodConstraints}
    
    urlPattern = "{locale}/{year}"
    
    reportRoute = New Route(urlPattern, New ReportRouteHandler)
    reportRoute.Constraints = New RouteValueDictionary(constraintValues)
    
    routes.Add(reportRoute)
End Sub

注釈

HttpMethodConstraintクラスを使用すると、ルートマッチングを特定の HTTP 動詞に制限できます。 たとえば、要求 POSTの HTTP 動詞が の場合にのみ、ルートが URL の一致と見なされるように指定できます。

ルート照合の制約を設定するには、 クラスの プロパティを ConstraintsRoute クラスの RouteValueDictionary インスタンスに設定します。 HTTP 動詞制約を設定するには、1 つのディクショナリ要素の値を HttpMethodConstraint オブジェクトに設定し、キーを任意の名前に設定します。

メソッドでは、ルートが HttpMethodConstraint URL と一致するかどうかを決定するときに、ルーティングで許可されるすべての HTTP 動詞 ASP.NET 含めます。

コンストラクター

HttpMethodConstraint(String[])

ルートに使用できる HTTP 動詞を使用して HttpMethodConstraint クラスの新しいインスタンスを初期化します。

プロパティ

AllowedMethods

ルートに使用できる HTTP 動詞のコレクションを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
Match(HttpContextBase, Route, String, RouteValueDictionary, RouteDirection)

ルートに許可されている動詞のいずれかである HTTP 動詞を使って要求が行われたかどうかを判断します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

IRouteConstraint.Match(HttpContextBase, Route, String, RouteValueDictionary, RouteDirection)

このメンバーの詳細については、「Match(HttpContextBase, Route, String, RouteValueDictionary, RouteDirection)」をご覧ください。

適用対象

こちらもご覧ください