Routes and Verbs ServiceStack

I am trying to install a coding pattern (some rules) in the services that we create for our business. We follow the basic guidelines outlined by apigree for designing RESTful services.

One of the rules that we would like to adopt is to block certain route and verb combines that we don’t want to support. Supposedly we will send HttpStatusCode.NotSupported back for these illegal combos and possibly for human reading?

The legal combos we want to support are:

GET /resource - lists all resources
GET /resource/{id} - retrieves specific resource by ID
POST /resource - adds a new resource 
PUT /resource/{id} - updates specific resource by ID
DELETE /resource/{id} - deletes specific resource by ID

There are some illegal combos that we clearly don't want to support.

POST /resource
PUT /resource
DELETE /resource

We have validators for each of the supported routes. But we do not have any of these illegal routes defined anywhere in our code base.

, GET /resource/{id} (string.Empty), ServiceStack (GET /resource/{id}), GET/. , DELETE /resource/{id} PUT /resource/{id}. , HttpStatusCode.NotSupported , API ( ).

?

+3
1

, , , :

[Route("/resource", "GET")]
public class GetAllResources {}

[Route("/resource/{Id}", "GET")]
public class GetResource 
{
    public int? Id { get; set; }
}

[Route("/resource", "POST")]
public class CreateResource { ... }

[Route("/resource/{Id}", "PUT")]
public class UpdateResource 
{
    public int Id { get; set; }
    ...
}

[Route("/resource/{Id}", "DELETE")]
public class DeleteResource 
{
    public int Id { get; set; }
    ...
}

:

public class ResourceServices : Service
{
    public object Get(GetResources request) { ... }
    public object Get(GetResource request) { ... }
    public object Post(CreateResource request) { ... }
    public object Put(UpdateResource request) { ... }
    public object Delete(DeleteResource request) { ... }
}

, , 404 NotFound, , .

, , :

[Route("/resource", "DELETE PUT")]
public class IllegalActions {}

public class ResourceServices : Service
{
    public object Any(IllegalActions request) 
    {
        return new HttpError(HttpStatusCode.NotAcceptable, "ActionNotSupported");
    }
}

- Error, ServiceStack. # HTTP :

SetConfig(new HostConfig { 
    MapExceptionToStatusCode = {
        { typeof(NotImplementedException), (int)HttpStatusCode.NotAcceptable },
        { typeof(NotSupportedException), (int)HttpStatusCode.NotAcceptable },
    }
});
+5

All Articles