HttpMethodConstraint(String[]) 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用允许用于该路由的 HTTP 谓词初始化 HttpMethodConstraint 类的新实例。
public:
HttpMethodConstraint(... cli::array <System::String ^> ^ allowedMethods);
public HttpMethodConstraint (params string[] allowedMethods);
new System.Web.Routing.HttpMethodConstraint : string[] -> System.Web.Routing.HttpMethodConstraint
Public Sub New (ParamArray allowedMethods As String())
参数
- allowedMethods
- String[]
对该路由有效的 HTTP 谓词。
例外
allowedMethods
参数为 null
。
示例
下面的示例演示了一个 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