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 物件,其 Constraints 屬性包含名為 且具有值為 httpMethod 類別實例 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 動詞命令。 例如,您可以指定只有在要求的 HTTP 動詞命令為 POST 時,路由才會被視為 URL 的相符專案。

若要設定路由比對的條件約束,請將 類別的 Route 屬性設定 Constraints 為 類別的 RouteValueDictionary 實例。 若要設定 HTTP 動詞條件約束,您可以將一個字典元素的值設定為 HttpMethodConstraint 物件,並將索引鍵設定為任何名稱。

在 方法中 HttpMethodConstraint ,您會在 ASP.NET 路由判斷路由是否符合 URL 時,包含路由允許的所有 HTTP 動詞。

建構函式

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)

適用於

另請參閱