Ambiguous Actions

醉酒当歌 提交于 2020-01-14 04:12:09

问题


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());
    }

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



回答2:


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



来源:https://stackoverflow.com/questions/2132926/ambiguous-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!