Base solution for your next web application
Open Closed

Routing with Sub- or nested ressources #364


User avatar
0
ddnils created

I am trying to create a nested route for one of my controllers.

I have tried this:

RouteConfig:

routes.MapHttpRoute(
                name: "InhabsApi",
                routeTemplate: "api/rooms/{id}/inhabitants/{inhabid}",
                defaults: new { inhabid = RouteParameter.Optional });

Controller looks like this:

[Abp.WebApi.Authorization.AbpApiAuthorize]
        public IHttpActionResult Get(long id)
        {
            return Ok(_roomService.GetAllInhabs(id));
        }

Unfortunatelly it does not work. I read Attribute Routing could help, but I am not sure if this works with Abp. Any help would be very appreciated :)


4 Answer(s)
  • User Avatar
    0
    ddnils created

    It did help to specify the controller in the route:

    routes.MapHttpRoute(
                    name: "InhabsApi",
                    routeTemplate: "api/rooms/{id}/inhabitants/{inhabid}",
                    defaults: new { controller = "Inhabitants", inhabid = RouteParameter.Optional });
    

    Problem solved. But anyways, what do you think about Attribute Routing?

  • User Avatar
    0
    ddnils created

    I can't get this to work with PUT

    It works with GET and POST though....

    not sure, what the problem is

  • User Avatar
    0
    hikalkan created
    Support Team

    It seems related to Web API, not ABP. Because ABP has nothing to prevent it as I know. Is same code working in another project without ABP?

  • User Avatar
    0
    ddnils created

    It works! Finally got it working.

    It was a rather stupid mistake, which i did oversee. I use Postman (chrome plugin) for testing and did provide a Content-Type = application/javascript instead of application/json

    The answer is: Use Content-Type = application/json This works, thank you for your answer!