暧昧动作 [英] Ambiguous Actions

查看:198
本文介绍了暧昧动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以有不同的参数多个动作?我已经看到了使用 HttpPost 动词标志,但它似乎并没有在其他地方为我工作。

当前请求有关控制器类型FoldersController`行动列表不明确以下操作方法之间。

 公众的ActionResult名单()
{
 // ...
}

公众的ActionResult列表(日期时间开始)
{
 // ...
}

公众的ActionResult列表(字符串键)
{
 // ....
}
 

尝试这种路线放慢参数我发现...

我还是有点困惑如何路由工作。这是我到目前为止所。 <一href="http://stackoverflow.com/questions/894779/asp-net-mvc-routing-via-method-attributes">http://stackoverflow.com/questions/894779/asp-net-mvc-routing-via-method-attributes

不过,我仍然得到了明确的错误。这不会使一个很大的意义对我来说 - 他们是两个完全不同的路线 - 它应该知道的完全的其中的ActionResult来召唤。但它不是这样做......

  [UrlRoute(PATH =列表/天/ {天})]
    [UrlRouteParameterConstraint(名称为天,正则表达式= @\ D +)]
    公共PartialViewResult列表(INT天)
    {
        返回PartialView(目录,Folders.List());
    }

    [UrlRoute(PATH =列表/戒指/ {}环)]
    [UrlRouteParameterDefault(名称为环,值=所有)
    公共PartialViewResult列表(串环)
    {
        返回PartialView(目录,Folders.List());
    }
 

解决方案

您需要给请求路由机制,足够的信息,能够选择哪一个适用非含糊,例如,通过在路由注册提供一个正则表达式模式并具有过滤某些请求在其中你会打电​​话 ListByDate

其他操作

但总的来说,如果东西开始变得混乱,程序,ti'll混淆使用: - 的http://odeto$c$c.com/Blogs/scott/archive/2010/01/25/kiss-your-asp-net-mvc-routes.aspx

因此​​,这避免了炮制正则表达式的另一种方法来消除歧义日VS'一切'通过正则表达式的行动是有一个路由方案: -

  • /按日期/ YY-MM-DD
  • /按键/键

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

解决方案

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

这篇关于暧昧动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆