Question

How can I setup a custom route to ignore everything after the id?

Autoroute appears to be setting up an alias when its created and saving it to the database, so it doesn't ignore anything, it must match exactly. And IRouteProvider appears to be for module controllers, where as I'd like to use the default content controllers.

So basically, I'd like to create action urls like

test/{Content.Id}/{Content.Slug}

and I'd like to match on the id, so

test/{Content.Id}

and at the same time ignore anything after the id, that may not even match the original slug, so

test/{Content.Id}/what/ever/garbage/gets/passed

For an example, look at how SO manages this url: https://stackoverflow.com/questions/22560272/aldjg%20jadf%20jg/adjf%20gadf%20g

Again, I'd like to use the default Orchard Content Type controllers (at least as long as I can).

Was it helpful?

Solution 2

I resolved this by adding a Routes class in my module /w the following code:

public class Routes : IRouteProvider
{
    public void GetRoutes(ICollection<RouteDescriptor> routes)
    {
        var routeDescriptors = GetRoutes();
        foreach (var descriptor in routeDescriptors)
            routes.Add(descriptor);
    }

    public IEnumerable<RouteDescriptor> GetRoutes()
    {
        return new[] {
            new RouteDescriptor {
                Name= "PostIdRoute",
                Route = new Route(
                    "post/{id}/{*SeoFluff}",
                    new RouteValueDictionary{
                        {"area", "Contents"},
                        {"controller", "Item"},
                        {"action", "Display"}
                    },
                    new RouteValueDictionary(),
                    new RouteValueDictionary { {"area", "Contents"} },
                    new MvcRouteHandler()
                )
            }
        };
    }
}

Notice the "{*SeoFluff}" part. That wildcard is the key to handling an unknown number of slashes, if you don't include that, you'll need to add multiple RouteDescriptors.

OTHER TIPS

You should probably have to use IRouteConstraint.
If by "Content Type Controllers", you mean: Part Drivers. Is it exactly the same thing. You do whatever you have to do to validate the URL and then call _contentManager.BuildDisplay(contentItem);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top