aspnet-mvc
  1. aspnet-mvc-custom-route-constraints

Custom Route Constraints - (ASP.NET MVC Routing)

ASP.NET MVC Routing allows you to define custom constraints for your routes, which can be used to restrict the values that can be accepted by a parameter. In this tutorial, we'll discuss what custom route constraints are, how to create them, and how to use them in an ASP.NET MVC application.

Syntax

The syntax for creating a custom route constraint in ASP.NET MVC is as follows:

public class MyConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        // add logic to check if the value is valid
    }
}

Example

Let's create a custom route constraint that ensures that a parameter is an integer greater than 0. The following code defines a custom PositiveIntegerConstraint class that implements the IRouteConstraint interface:

public class PositiveIntegerConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        int value;
        if (int.TryParse(values[parameterName].ToString(), out value) && value > 0)
        {
            return true;
        }

        return false;
    }
}

We can then use this custom constraint in a route definition as follows:

routes.MapRoute(
    name: "Blog",
    url: "blog/{year}/{month}/{day}",
    defaults: new { controller = "Blog", action = "Index" },
    constraints: new { year = new PositiveIntegerConstraint(), month = new PositiveIntegerConstraint(), day = new PositiveIntegerConstraint() }
);

This route definition ensures that all of the {year}, {month}, and {day} parameters are positive integers.

Explanation

Custom route constraints in ASP.NET MVC allow you to specify custom logic for validating route parameters. When a request is processed, the custom route constraint is executed to determine if the value of the parameter satisfies the constraint. If the constraint is not satisfied, the route is ignored and the next matching route is searched.

Use

Custom route constraints can be used in ASP.NET MVC to restrict the values that can be accepted by a parameter. This can be useful for enforcing business rules or ensuring that only valid data is passed to a controller action.

Important Points

Here are some important points to keep in mind when using custom route constraints in ASP.NET MVC:

  • Custom route constraints are executed for each incoming request, so their performance should be optimized.
  • Constraints can be combined using the RouteConstraint attribute for complex validation scenarios.
  • Custom constraints can be created for any data type, not just integers.

Summary

In this tutorial, we discussed custom route constraints in ASP.NET MVC, which allow you to restrict the values that can be accepted by a parameter. We covered the syntax, example, explanation, use, and important points of custom route constraints in ASP.NET MVC. With this knowledge, you can create custom constraints to validate route parameters and enforce business rules in your application.

Published on: