1

This is a more specific version of another of my questions: Restful MVC Web Api Inheritance, I hope an answer to this will help me answer that.

Im using ASP.NET web api,
I want to be able to route something like this: [{object}/{id}]/{controller}/{id}.
so i want an array of objects with optional /{id} ending with the 'api endpoint'.

I want to be able to route these:
/houses
/houses/3
suburbs/3/houses
council/5/suburbs/houses
city/8/council/suburbs/houses
ETC

TO

get(List<restRoute>parents, int id){
    ...
}

restRoute would be an object with a string for the object and an optional int (or guid etc) for the id

Does anyone know where i can start?
I don't want to route every single one individually.

13
  • I don't see how the 2 last urls you have shown correspond to the route. According to your route definition after the controller name you could only have an id. Also it's not clear why are you using List<object> as your action parameter. How do you expect the model binder to know what to do with this object? Commented Jul 21, 2012 at 12:59
  • Maybe you could generate the routes dynamically in RegisterRoutes in global.asax? Otherwise, I have no idea... Commented Jul 21, 2012 at 13:12
  • @DarinDimitrov i said the /{id} is optional. And i addressed the list object by editing the question slightly. Commented Jul 21, 2012 at 13:18
  • @TheJonasPersson How would you do that? Commented Jul 21, 2012 at 13:18
  • Which id is optional? Only the last token of a route can be optional. Commented Jul 21, 2012 at 13:18

2 Answers 2

2

I had also such problems with routing from the box in ASP.NET MVC. Its good way to be used as common routing, but is not so flexible for custom routs.

In WCF Web Api (ASP.NET web api in CTP version) was used attribute based routing.

I think its more flexible, but as negative point - each method should have routing attribute.

Take a look at this blog post:

http://www.strathweb.com/2012/05/attribute-based-routing-in-asp-net-web-api/

It describes how to implement attribute based routing using ASP.NET Web Api. Because such approach is more flexible for routes you can map to methods, it can be helpful for you.

Sign up to request clarification or add additional context in comments.

2 Comments

I've re-read that article a few times now. It makes what I want to do easier, but it isn't perfect since I'll have to manually add each possible attribute to each method. Having said that this is a good answer, so +1.
This attribute based routing library is open source with MIT license. As positive, you can look at this implementation as example and then implement your similar generic routing for your solution. Source code is hosted on Git github.com/mccalltd/AttributeRouting
1

You could use the {*anything} Variable Segmented URL pattern in your route and handle the splitting up and figuring out of what part of the url corresponds to what bit of data in your method:

Global.asax:

routes.MapRoute(
            "Special", // name
            "{*allthethings}", // parameters
            new { controller = "Special", action = "Sauce" } // defaults
        );

SpecialController:

public ActionResult Sauce()
{
    string data = RouteData.Values["allthethings"].ToString();
    string[] items = data.Split('/');
    foreach (string item in items)
    {
        // do whatever you need to figure out which is what!
    }

    return View();
}

If you wanted to be a bit cleverer about it you could create your own custom RouteHandler to do the splitting. Something like David Ebb's PK routehandler would probably do the trick, with some customisation to fit your requirements in the processing of the route. You could use this to split up the "allthethings" parameter and turn it into your List<RestRoute> format before passing the request on to the Controller

1 Comment

I ended up using this approach, it worked just fine as it allowed me to use a factory architecture with a single controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.