Daniel Leiszen
Daniel Leiszen

Reputation: 1897

Orchard controller not called using IRouteProvider and RouteAttribute

I am using IRouterProvider for creating route description from attribute routing as suggested in this thread. The router provider is called and the routes are fetched.

I generate links for these specific actions by using Url.Action helper method, and the urls are generated for the custom routes as expected. However, when such a link is navigated the controller is not called. I got plain 404. Orchard is not even in the game.

My route provider looks like:

public IEnumerable<RouteDescriptor> GetRoutes()
{
    var type = typeof(ShoppingCartController);

    foreach (var meth in type.GetMethods())
    {
        var attr = meth.GetCustomAttribute<System.Web.Mvc.RouteAttribute>();

        if (attr != null)
        {
            yield return new RouteDescriptor
            {
                Priority = 100,
                Name = string.Format("ShoppingCart{0}Route", meth.Name),
                SessionState = System.Web.SessionState.SessionStateBehavior.Required,
                Route = new Route(attr.Template,
                    new RouteValueDictionary 
                    {
                        { "area", "XXXX.Features" },
                        { "controller", "ShoppingCart" },
                        { "action", meth.Name }
                    },
                    new RouteValueDictionary(),
                    new RouteValueDictionary 
                    {
                        { "area", "XXXX.Features" }
                    },
                    new MvcRouteHandler())
            };
        }
    }
}

My controller method signature is:

[Themed]
[Route("cart/checkout")]
public ActionResult CheckOut() {...}

I tried to debug the problem, but I don't even know where to start. I am quite new to Orchard.

Thanks in advance

Upvotes: 2

Views: 405

Answers (1)

Piotr Szmyd
Piotr Szmyd

Reputation: 13366

The code in route provider seems ok. There are a few things you can check:

  • Hardcode the same route without using the RouteAttribute and see if this one gets through. If so, then the attribute itself messes things up for some reason and the best option would be to mimic the attribute class and use that one instead to decorate actions.
  • If the above won't work then the issue is within the route itself. In that case:
    • Make sure the area parameter is correct and equals to the module (not feature) name your controller is in (ie. the module project name).
    • Do the same for controller and action names
    • Set the route priority to int.MaxValue to make sure it's not hijacked by some other route
    • Try not using the Name property and/or make sure it's really unique

Upvotes: 1

Related Questions