views:

81

answers:

2

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. http://stackoverflow.com/questions/894779/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());
    }
+1  A: 

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
I am not sure what you mean. How do I point it to the appropriate method with the regex?
Stacey
@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
Would something similar to this be wise? http://stackoverflow.com/questions/894779/asp-net-mvc-routing-via-method-attributes
Stacey
I have updated the question with a bit more information.
Stacey
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
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.
Stacey
Unfortunately I GTG. I recommend ASP.NET MVC in action as a good walkthrough that doesnt overexplain the basics (or overcomplicate it!)
Ruben Bartelink
You mention putting an attribute for an "ActionAlias"? I've never heard of this before.
Stacey
read the book, didn't get much from it. This routing stuff is not explained very well anywhere I've looked.
Stacey
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
If this is all worse than useless, I'll delete in a few hours...
Ruben Bartelink
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?
Stacey
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.
Stacey
Good to hear you got things sorted.
Ruben Bartelink
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.
Stacey
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.
Stacey
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
+1  A: 

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
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