Discussing Web API2 Route Constraints

Introduction

In this short discussion, we are going to understand Route Contraints in Web API. As this is a short content article, our main focus will be on the description of Route Contraints.
In the end of discussion, we will create a Custom Route Constraints

What are Route Constraints?

Before going to discuss about Route Constraints lets take a look into attribute routing. In very short version routing is nothing just a way to use our Web API matches a URI to an action.
In new version of Web API i.e. Web API2 there is a new type of routing which named as attribute routing.

In attribute routing, we define attributes to create route. With the use of this one can define the hierarchy of resources of API and its easy to handle/controle.

Take a look into few code-snippet:

[Route("serverdata/{type}")]
public IEnumerable<ServerData> GetServerData(int type)
{
    return ServerDataRepository.GetAll().Where(d => d.Type == type);
}
[Route("serverdata/{zone}")]
public IEnumerable<ServerData> GetServerData(string zone)
{
    //soem stuff here
}

In this manner we can easily define the URI pattern and manage/handle our resources.

Now, imagine you have such a complex or long URI, where you can use your parameter in any type (like int or string), refer following code-snippet as an example:

[Route("serverdata/{type}/datadetails")]
public IEnumerable<DataDetail> GetDataDetailsByType(int type)
{
    //some stuff here
}

Now, whenever request comes,Web API tried to match URI "serverdata/{type}/datadetails" template definedin route. We can pass different values to make this template valid:

- http://127.0.0.1/serverdata/external/datadetails
- http://127.0.0.1/serverdata/1/datadetails
- http://127.0.0.1/serverdata/1oT/datadetails

How ut works?

In above, URI route template our {type} is a parameter (I oftnely called it as URI placeholder), when we pass the value it automatically assign the same to our action method's parameter.
Imagine, where we are passing "external" in URI template, our template matches but actually our paramter is int. You can imagine, what is wrong here?

Here, we reached at our main discussion Route Contraints in Web API, this is nothing but provides a facility to restrict paramaters so, Web API match correct URI route template.
In route just need to define a constraint with the parameter eg. "{parameter:constraint}".

Mostly available/used constraints

Here, I try to include almost all route contraints:

1. int constraints - restricts to 32-bit integer value

[Route("serverdata/{typeid:int}")]
public IEnumerable<ServerData> GetServerDataByType(int typeid)
{
    //some stuff here
}

2. double constraints - restricts to 64-bit floating value

[Route("serverdata/{typeconst:double}")]
public ServerData GetCalculatedDataFrequency(double typeconst)
{
    //some stuff here
}

3. float constraints - restricts to 32-bit floating value

[Route("serverdata/{typeconst:float}")]
public ServerData GetCalculatedDataFrequency(float typeconst)
{
    //some stuff here
}

Note: I suggest, try double and float constraints

4. guid constraints - restricts to GUID value

[Route("serverdata/{id:guid}")]
public ServerData GetServerDataById(Guid id)
{
    //some stuff here
}

5. length constraints - restricts string to specified length or specified length range

[Route("serverdata/{passcode:length(8)}")]
public String GetDatabasename(string passcode)
{
    //some stuff here
}
[Route("serverdata/{passcode:length(4,18)}")]
public String GetDatabasename(string passcode)
{
    //some stuff here
}

6. long constraints - restricts to 64-bit integer value

[Route("serverdata/{typeid:long}")]
public IEnumerable<ServerData> GetServerDataByType(long typeid)
{
    //some stuff here
}

7. alpha constraints - restricts to upper/lower case english alphabets

[Route("serverdata/{spname:alpha}")]
public IEnumerable<ServerData> GetServerDataByType(string spname)
{
    //some stuff here
}

8. bool constraints - restricts to boolean value (true/false)

[Route("serverdata/{isDeleted:bool}")]
public IEnumerable<Records> GetDeletedRecords(bool isDeleted)
{
    //some stuff here
}

9. datetime constraints - restricts to DateTime value

[Route("serverdata/{createdon:datetime}")]
public IEnumerable<Records> GetRecordsoByCreatedDate(DateTime createdon)
{
    //some stuff here
}

10. decimal constraints - restricts to decimal value

[Route("serverdata/{amount:decimal}/employee")]
public IEnumerable<EmployeeData> GetEmployeebySalary(Decimal amount)
{
    //some stuff here
}

11. range constraints - restricts an integer within a range of value

[Route("serverdata/{typeid:range(3,15)}")]
public IEnumerable<EmployeeData> GetEmployeebyType(int typeid)
{
    //some stuff here
}

Apart from above there are few more like regex, maxlength, minlength, I am not going to discuss about these as these are as clear as their names 🙂

Creating Custom Route Constraints

There are many Route constraints available (as discussed in preceding code), but there might be scenarios, where we need some special kind of constraints. To fulfill those requirements we can create our own constraints.

In the very first step for creation of Custom Route Constraints, we need to implement interface IHttpRouteConstraint.

I am taking an example of my live project where we have to restrict bad-words:

Following code-snippet depicts a custom constraints:

public class NoBadWordsConstraint : IHttpRouteConstraint
{
    public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, 
        IDictionary<string, object> values, HttpRouteDirection routeDirection)
    {
        object value;
	string[] badWords=new string[]{"bad1","bad2","bad3"};
        if (values.TryGetValue(parameterName, out value) && value != null)
        {
            string stringValue;
            if (value is string)
            {
                stringValue = (string)value;
                foreach(var w in badWords)
                {
                  if(w.Contains(stringValue)) return false;
                }
                return true;
            }

        }
        return false;
    }
}

To use this, we have to tell Web API2 that it exists so, we need to register it first
Add following lines in your WebApiConfig class in Register method

var defaultconstraintResolver = new DefaultInlineConstraintResolver();
defaultconstraintResolver.ConstraintMap.Add("nobadword", typeof(NoBadWordsConstraint));
config.MapHttpAttributeRoutes(defaultconstraintResolver);

Following is the code-snippet, showing how we can use it:

[Route("serverdata/{word:length(10)}")]
public IEnumerable<EmployeeData> GetEmployeebyword(string word)
{
    //some stuff here
}

Closing notes

In this short content article, we discussed Route constraints, types of contsraints and finally, we created a custom constraint.

Leave A Reply