1

Is there any way to have multiple actions with different parameters? I've seen it using the HttpPost verbs flag, but it doesn't seem to work for me in other places.

The current request for action List on controller type FoldersController` is ambiguous between the following action methods.

public ActionResult List()
{ 
 //... 
}

public ActionResult List(DateTime start)
{
 // ...
}

public ActionResult List(string key)
{
 // ....
}

Trying this Route Paramter I found on ...

I'm still a bit confused about how the routing works. This is what I have so far. ASP.NET MVC Routing Via Method Attributes

But I still get the ambiguous error. This doesn't make a lot of sense to me - they are two entirely different routes - it should know exactly which ActionResult to call forth. But it isn't doing that...

    [UrlRoute(Path = "List/Days/{days}")]
    [UrlRouteParameterConstraint(Name = "days", Regex = @"\d+")]
    public PartialViewResult List(int days)
    {
        return PartialView("List", Folders.List());
    }

    [UrlRoute(Path = "List/Rings/{ring}")]
    [UrlRouteParameterDefault(Name = "ring", Value = "all")]
    public PartialViewResult List(string ring)
    {
        return PartialView("List", Folders.List());
    }
Community
  • 1
  • 1
Ciel
  • 17,312
  • 21
  • 104
  • 199
  • Re the UrlRoute stuff 1) Dont like it 2) are you calling the route registration stuff that library provides (that would explain it not working) ? – Ruben Bartelink Jan 25 '10 at 15:31
  • // register any other routes from their attributes. RouteUtility.RegisterUrlRoutesFromAttributes(routes); in the global.asax file. – Ciel Jan 25 '10 at 15:33
  • I'm not sure what you mean by 'calling the route registration stuff that the library provides'. I'm trying to figure out how to map the route directly from the registerroutes method, just a slow trip in getting there... – Ciel Jan 25 '10 at 15:34
  • In the stuff you linked to, there's a RouteUtility.RegisterUrlRoutesFromAttributes(routes); - just chekcing that was called as UrlRoute Attribute means nothing without it [which would explain no change in behavior]. – Ruben Bartelink Jan 25 '10 at 15:43
  • So you have that, and the two routes are still not being picked up? Have you dumped out the registered routes to see if they are indeed in there? – Ruben Bartelink Jan 25 '10 at 15:48
  • Yes. I've also tried registering the routes manually in the .asax file. They're just plain outright not being picked up. I've also tried appending {area} to the route to see if that changed it. They're within an area. – Ciel Jan 26 '10 at 21:21

2 Answers2

1

You need to give the request routing mechanism enough information to be able to pick which one applies non-ambiguously, e.g., by supplying a regex pattern in the route registration and having that filter some of the requests into another action which you'd call ListByDate.

But in general if stuff starts getting confusing to program , ti'll be confusing to use:- http://odetocode.com/Blogs/scott/archive/2010/01/25/kiss-your-asp-net-mvc-routes.aspx

So another approach which avoids having to concoct regexes to disambiguate the date vs 'everything else' actions via a regex is to have a routing scheme:-

  • /by-date/yy-mm-dd
  • /by-key/key
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • I am not sure what you mean. How do I point it to the appropriate method with the regex? – Ciel Jan 25 '10 at 14:41
  • @Stacey. in your `routes.MapRoute` you'd add `action="ListByDate"` and then either rename your Action to that name or put an attribute on it to say "I implement that action alias". But you'd need to have a regex that matches how the DateTime [model] binding normally works which is difficult to do cleanly. – Ruben Bartelink Jan 25 '10 at 15:03
  • Would something similar to this be wise? http://stackoverflow.com/questions/894779/asp-net-mvc-routing-via-method-attributes – Ciel Jan 25 '10 at 15:17
  • I personally dont like the look of the stuff in the question you link to. Do you have that library and the call to it in there? At the end of the day that's not going to make the disambiguation you're looking for (if it looks like a date call this one, otherwise call the other one). Note you could also do tricks with Model Binders, but all of these things are straying very far from simplicity. – Ruben Bartelink Jan 25 '10 at 15:30
  • It's a lot simpler to me to have a single method and pass it different parameters, or get to it through different routes, than to make 3 or 4 different named methods for similar tasks. – Ciel Jan 25 '10 at 15:33
  • Unfortunately I GTG. I recommend ASP.NET MVC in action as a good walkthrough that doesnt overexplain the basics (or overcomplicate it!) – Ruben Bartelink Jan 25 '10 at 15:33
  • You mention putting an attribute for an "ActionAlias"? I've never heard of this before. – Ciel Jan 25 '10 at 15:36
  • read the book, didn't get much from it. This routing stuff is not explained very well anywhere I've looked. – Ciel Jan 25 '10 at 15:37
  • ActionAlias certainly isnt a well defined term. What I'm referring to is the way that a route can have action="XXXX" on the end. This tag can then be used in the attribute you attach to the Action method, i.e., saying "This is the Method that handles action=XXXX". Dont have the book to hand right now but its section on defining routes with regexes is pretty clear from memory? – Ruben Bartelink Jan 25 '10 at 15:42
  • No, it isn't useless. The book was a bit difficult to work with, that was my reference. I've been tinkering around with it and I'm just not sure why it isn't working. Would the injection of Areas (MVC 2.0) be adding a bunch of problems to the routing? – Ciel Jan 26 '10 at 20:13
  • Thank you for your help, I have discovered the problem was in my regular expressions and routing scheme. on the server, I had a sub-folder that the content was published to - this affected routing of pretty much everything. – Ciel Jan 28 '10 at 17:58
  • Good to hear you got things sorted. – Ruben Bartelink Jan 29 '10 at 13:46
  • The confusion I am running into is something of a throwback to ASP.NET web forms, and typical programming - where methods are differentiated based on their signatures, not just their names. Obviously, due to the rather non-objective nature of http postback, it clearly isn't possible for ASP.NET MVC to do this. How does it know whether something is just an integer, a string, or a date? Well - I've come to find out that quite simply it doesn't. This should have been excessively clear from the beginning but for some reason it just never occurred to me. – Ciel Jan 31 '10 at 15:18
  • I never meant to make the statement that any of the information was useless, and I feel my words were a bit misinterpreted - I simply meant that of the books I've read - including the one you referenced, I haven't seen anyone just come out and say it quite plainly. They all assume some kind of history with Ruby, or other url routing - at least this is how I felt when reading through them. The routing stuff has been very confusing in that examples seem to make a lot of assumptions. I thank you greatly for your assistance on this, it has really bothered me for a while and I'm glad it is over. – Ciel Jan 31 '10 at 15:21
  • Take it easy - I wasnt gettiung agressive; just wondering whether the to/fro was proving useful or not given that a relatively simple issue was taking a long time to be resolved. Have you looked at Model Binders? The fact that they're in the mix at the same end of procesing as the routing bit probably doesnt help from the point of view of deciding how to manage your question. I personally have never done any Ruby work and really liked the MVCIA book, but different people click with different styles of books at different times - in some cases some need a primer book first etc. – Ruben Bartelink Feb 01 '10 at 08:47
1

Since you don't have the AcceptVerbs set it can't figure out which method to call. Can you clarify "it doesn't seem to work for me in other places"?

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
  • The English is indeed ambiguous. I interpreted the statement as trying to say "I know you can use AcceptVerb to disambiguate and that is normally fine. In this case it doesnt help [as I want to tie both to GET]" – Ruben Bartelink Jan 25 '10 at 15:18