Route.Constraints Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur kamus ekspresi yang menentukan nilai yang valid untuk parameter URL.
public:
property System::Web::Routing::RouteValueDictionary ^ Constraints { System::Web::Routing::RouteValueDictionary ^ get(); void set(System::Web::Routing::RouteValueDictionary ^ value); };
public System.Web.Routing.RouteValueDictionary Constraints { get; set; }
member this.Constraints : System.Web.Routing.RouteValueDictionary with get, set
Public Property Constraints As RouteValueDictionary
Nilai Properti
Objek yang berisi nama dan ekspresi parameter.
Contoh
Contoh berikut menunjukkan cara membuat Route objek dan mengatur Constraintsproperti , DataTokens, dan Defaults .
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
reportRoute.Defaults = new RouteValueDictionary { { "locale", "en-US" }, { "year", DateTime.Now.Year.ToString() } };
reportRoute.Constraints = new RouteValueDictionary { { "locale", "[a-z]{2}-[a-z]{2}" }, { "year", @"\d{4}" } };
reportRoute.DataTokens = new RouteValueDictionary { { "format", "short" } };
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
urlPattern = "{locale}/{year}"
reportRoute = New Route(urlPattern, New ReportRouteHandler)
reportRoute.Defaults = New RouteValueDictionary(New With {.locale = "en-US", .year = DateTime.Now.Year.ToString()})
reportRoute.Constraints = New RouteValueDictionary(New With {.locale = "[a-z]{2}-[a-z]{2}", .year = "\d{4}"})
reportRoute.DataTokens = New RouteValueDictionary(New With {.format = "short"})
routes.Add(reportRoute)
End Sub
Contoh berikut menunjukkan Route objek yang propertinya Constraints berisi parameter bernama httpMethod
dengan instans HttpMethodConstraint kelas untuk nilai .
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
Keterangan
Properti Constraints memungkinkan Anda membatasi nilai yang dapat diterima untuk parameter URL yang diproses oleh rute. Anda menetapkan RouteValueDictionary objek ke Constraints properti . Setiap elemen dalam RouteValueDictionary objek berisi nama parameter dan salah satu hal berikut:
String yang menentukan ekspresi reguler. Ekspresi reguler tidak peka huruf besar/kecil.
Objek yang mengimplementasikan IRouteConstraint antarmuka dan yang mencakup Match metode .
Kelas HttpMethodConstraint menerapkan antarmuka IRouteConstraint. Anda dapat menyertakan instans HttpMethodConstraint kelas sebagai batasan untuk menentukan kata kerja HTTP mana yang dapat diterima untuk rute.