1

I am suffering from the following problem when trying to perform a POST to create a new REST resource on a nested URL with my own .Net WebApi based REST-service.

Please imagine the following url api/parents/{parentId}/children on which I want to perform a POST to add another child.

This is very similar to the problem described in: Using asp webapi to post a nested resource. But in global.asax I do not succeed to define with routes.MapHttpRoute(...) a route which calls a method of my Parents-controller containing the {parentId} as well as the Child object to be added/posted, e.g. calling:

[HttpPost]
public HttpResponseMessage AddChildToParent(int parentId, Child newChild) {
    //implementation
}

(This is also the difference to the answer of the above mentioned question, since it does not provide any object which could be added.)

I was searching the Web, but unfortunately I could not find a solution - probably also, because I could not even find an official documentation of the semantics of the defaults-object used as parameter for the MapHttpRoute() method.

Can anybody please suggest a working routing definition for my problem or at least post a link to a useful documentation of the defaults-object used in MapHttpRoute()? Thanks a lot!

Community
  • 1
  • 1
Max
  • 11
  • 2
  • The answer in [this](http://stackoverflow.com/a/10030485) post may be helpful to solve your problem. – Miguel Mar 19 '13 at 02:18

1 Answers1

0

Try to add the following Web API routing to your registration function. (The registration function by default is passed in WebApiApplication on Application_Start, which inherits from System.Web.HttpApplication, to the function GlobalConfiguration.Configure(...). By default it's defined in the static class WebApiConfig.)

Add

config.Routes.MapHttpRoute(
    name: "PostChild",
    routeTemplate: "api/{controller}/{parentId}/children",
    defaults: new { action = "AddChildToParent" },
    constraints: new
    {
        httpMethod = new HttpMethodConstraint(HttpMethod.Post),
        controller = "Parents"
    }
);

to the registration function, where config is the HttpConfiguration object parameter.

Then via the URL api/parents/{parentId}/children the following method in ParentsController will be called:

public class ParentsController : ApiController
{
    [HttpPost]
    public HttpResponseMessage Post([FromUri]int parentId, [FromBody] Child child) {
        //implementation
    }
}

Note that both the defaults and the constraints parameters in MapHttpRoute are anonymous objects with property names corresponding to Web API variable names. As the action variable is cannot be set via URL, it gets a default value which points to the web API method in the controller.

The constraints object limits the call to the given Regular Expression. In this case it is the fixed controller name, but could also be multiple controllers e.g. by "(Parents|ParentsAlternative)". Only Web API calls with all constraints met will lead to a Controller method being called, here AddChildToParent.

The Child object here is passed in the HTTP body as JSON, as denoted in the parameter attribute [FromBody].

FrKunze
  • 190
  • 3
  • 8