Orchard Web Api and custom routes

Topics: Core, Customizing Orchard, General
Sep 27, 2012 at 5:20 PM

Good morning,

    As we know, if you add a new web api controller named ValueController to module "foo", you can access that web api through: localhost:xxxx/api/foo/value. The aforementioned URL would hit the "GET" request on the controller.

    If I add a new HttpRouteDescriptor to the routes by creating a class and inheriting from IHttpRouteProvider, and that route is:  "/api/{controller}/{action}," I subsequently would not be able to access say, for example, localhost:xxxx/api/foo/bar ( the method on the web api controller has an "ActionName" decorator defining "bar" and the method is "getBar"). 

    Based on how Orchard handles Web Api and inserting the area name into the route explicitly, how would the above route be accessed or defined?

 

Thank you.

Coordinator
Sep 30, 2012 at 9:45 PM

Priorities?

Nov 25, 2012 at 10:41 AM
Edited Nov 25, 2012 at 10:43 AM

I am facing the same problem. Did you find a solution?

set the priority to -10, also tried 200

my routes.cs file is as following:

 

using System.Collections.Generic;
using Orchard.Mvc.Routes;
using Orchard.WebApi.Routes;

public class HttpRoutes : IHttpRouteProvider
{

    public void GetRoutes(ICollection<RouteDescriptor> routes)
    {
        foreach (RouteDescriptor routeDescriptor in GetRoutes())
        {
            routes.Add(routeDescriptor);
        }
    }

    public IEnumerable<RouteDescriptor> GetRoutes()
    {
        return new[] {
            new HttpRouteDescriptor {
                Name = "HelloApi",
                Priority = 200,
                RouteTemplate = "HelloApi",
                Defaults = new { 
                    area = "Orchard.YITWebServices", 
                    controller = "YITWebServices" 
                },
            }
        };
    }
}

should this show a result if I try to access the controller through my browser like this:
http://localhost:30320/api/Orchard.YITWebServices/HelloApi
or this http://localhost:30320/HelloApi
?
Neither is working
Thanks
Developer
Nov 25, 2012 at 11:06 AM

By defining a custom route, you actually remove the default api url... so you url in this instance would be.. http://localhost:30320/HelloApi

Can you post your controller in here?

Also are you inheriting off ApiController or Controller?

Nov 25, 2012 at 11:14 AM
Edited Nov 25, 2012 at 12:40 PM

Hi, I was able to access my controller by adding the project to the root of the solution, and setting it to be the startup- project

Is there a way to make it work by putting the project inside "Modules" and not in the root of the solution?

Developer
Nov 25, 2012 at 7:03 PM

Can you send me your module so I an see what you are trying to do? Jetski5822 at gmail dot com

Nov 26, 2012 at 2:41 AM

Hey, so I have this all working, here is my routeconfig.cs that is my module. You don't need to register this guy in the Orchard.Web global.asax, you just need to define it in your module and it gets wired up at runtime. Here is the code, I access it by doing: /api/person or api/person/1 or api/person/remove/1 or something like that:

public class RouteConfig : IHttpRouteProvider {
        public IEnumerable<RouteDescriptor> GetRoutes() {
            return new[] {
                             new HttpRouteDescriptor {
                                                         Name = "ControllerOnly",
                                                         RouteTemplate = "api/{controller}",
                                                         Defaults = new {
                                                                            area = "Rikkle"
                                                                        }
                                                     },
                             new HttpRouteDescriptor {
                                                         Name = "ControllerAndId",
                                                         RouteTemplate = "api/{controller}/{id}",
                                                         Defaults = new
                                                                        {
                                                                            area = "Rikkle"
                                                                        },
                                                         Constraints = new {id = @"^\d+s"}
                                                     },
                            new HttpRouteDescriptor {
                                                        Name = "ControllerAndAction",
                                                        RouteTemplate = "api/{controller}/{action}/{id}",
                                                         Defaults = new {
                                                                            area = "Rikkle",
                                                                            id = RouteParameter.Optional
                                                                        }
                                                    }

                         };
        }

        public void GetRoutes(ICollection<RouteDescriptor> routes) {
            foreach (RouteDescriptor routeDescriptor in GetRoutes()) {
                routes.Add(routeDescriptor);
            }
        }
    }

Developer
Nov 26, 2012 at 9:13 AM

Hi Julia,

I have replied to your email. - There was a few problems, 

1. Namespaces were incorrect.

2. Not using the CreateMessage code

3. URL your were calling was wrong.

Nick

Nov 26, 2012 at 11:52 AM
Edited Nov 26, 2012 at 11:53 AM

 

Hi Nick,

Event after installing the fixed module on a new&clean  version of orchard, it refuses to work.

Maybe I need to install something additional?

By "CreateMessage" you're referring to the return type of the get function?

Nov 26, 2012 at 1:38 PM
Edited Nov 26, 2012 at 1:38 PM

Success! Thank you Nick !

You're the best!! :)