路线:采取行动的当前请求[...]是下面的操作方法之间的暧昧 [英] Routing: The current request for action [...] is ambiguous between the following action methods

查看:95
本文介绍了路线:采取行动的当前请求[...]是下面的操作方法之间的暧昧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Browse.chtml 查看,在这里用户可以输入搜索词,或离开搜索项空白。当输入搜索词,我想页面直接到的http://本地主机:62019 /画廊/浏览/ {搜索关键词} 键,进入没事的时候,我想直接在浏览器中的http://本地主机:62019 /相册/浏览/开始/这里

当我尝试,我得到了错误:


  

行动浏览控制器类型GalleryController当前请求是下面的操作方法之间暧昧:
  System.Web.Mvc.ActionResult浏览(System.String)的类型AutoApp_MVC.Controllers.GalleryController
  在类型AutoApp_MVC.Controllers.GalleryController System.Web.Mvc.ActionResult浏览(的Int32,System.String)


一切我与MVC做是首次。我不知道还有什么尝试在这一点上。

 公众的ActionResult浏览(字符串ID)
{
    VAR摘要= / *搜索使用id作为搜索词* /
    返回视图(摘要);
}公众的ActionResult浏览(字符串名称1,字符串名称2)
{
    进入没事的时候VAR摘要= / *默认列表* /
    返回视图(摘要);
}

我也有这个在Global.asax.cs中:

  routes.MapRoute(
         StartBrowse
         画廊/浏览/ {S1} / {} S2
         新
         {
             控制器=画廊,
             行动=浏览,
             S1 = UrlParameter.Optional,
             S2 = UrlParameter.Optional
         });    routes.MapRoute(
         ActualBrowse
         画廊/浏览/ {}搜索关键词
         新
         {
             控制器=画廊,
             行动=浏览,
             搜索关键词= UrlParameter.Optional
         });


解决方案

您只能有一个最大的控制器2的操作方法具有相同的名称,而为了做到这一点,1必须 [HttpPost] ,另一个必须 [HTTPGET]

由于您的两个方法是GET,您应该重命名的动作方法之一或将其移动到不同的控制器。

虽然你的2浏览的方法是有效的C#重载,MVC的操作方法选择想不通调用哪个方法。它将尝试匹配方法(或反之亦然)的路由,并且这个algoritm不强类型

您可以完成你想要使用指向不同的操作方法定制路线是什么:

...在Global.asax中

  routes.MapRoute(//这条路必须首先声明,它下面的人之前
     StartBrowse
     画廊/浏览/开始/这里,
     新
     {
         控制器=画廊,
         行动=StartBrowse
     });routes.MapRoute(
     ActualBrowse
     画廊/浏览/ {}搜索关键词
     新
     {
         控制器=画廊,
         行动=浏览,
         搜索关键词= UrlParameter.Optional
     });

...并在控制器...

 公众的ActionResult浏览(字符串ID)
{
    VAR摘要= / *搜索使用id作为搜索词* /
    返回视图(摘要);
}公众的ActionResult StartBrowse()
{
    进入没事的时候VAR摘要= / *默认列表* /
    返回视图(摘要);
}

您可能还能够<一个href=\"http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc\">keep动作方法命名控制器一样,通过施加 [ActionName] 属性来一个区别。使用相同的Global.asax如上,控制器会再看看这样的:

 公众的ActionResult浏览(字符串ID)
{
    VAR摘要= / *搜索使用id作为搜索词* /
    返回视图(摘要);
}[ActionName(StartBrowse)]
公众的ActionResult浏览()
{
    进入没事的时候VAR摘要= / *默认列表* /
    返回视图(摘要);
}

I have a View called Browse.chtml, where the user can enter a search term, or leave the search term blank. When entering the search term, I want to direct the page to http://localhost:62019/Gallery/Browse/{Searchterm} and when nothing is entered, I want to direct the browser to http://localhost:62019/Gallery/Browse/Start/Here.

When I try this, I get the error:

The current request for action 'Browse' on controller type 'GalleryController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Browse(System.String) on type AutoApp_MVC.Controllers.GalleryController System.Web.Mvc.ActionResult Browse(Int32, System.String) on type AutoApp_MVC.Controllers.GalleryController

Everything I'm doing with MVC is for the first time. I'm not sure what else to try at this point.

public ActionResult Browse(string id)
{
    var summaries = /* search using id as search term */
    return View(summaries);
}

public ActionResult Browse(string name1, string name2)
{
    var summaries = /* default list when nothing entered */
    return View(summaries);
}

I also have this in Global.asax.cs:

    routes.MapRoute(
         "StartBrowse",
         "Gallery/Browse/{s1}/{s2}",
         new
         {
             controller = "Gallery",
             action = "Browse",
             s1 = UrlParameter.Optional,
             s2 = UrlParameter.Optional
         });



    routes.MapRoute(
         "ActualBrowse",
         "Gallery/Browse/{searchterm}",
         new
         {
             controller = "Gallery",
             action = "Browse",
             searchterm=UrlParameter.Optional
         });

解决方案

You can only have a maximum of 2 action methods with the same name on a controller, and in order to do that, 1 must be [HttpPost], and the other must be [HttpGet].

Since both of your methods are GET, you should either rename one of the action methods or move it to a different controller.

Though your 2 Browse methods are valid C# overloads, the MVC action method selector can't figure out which method to invoke. It will try to match a route to the method (or vice versa), and this algoritm is not strongly-typed.

You can accomplish what you want using custom routes pointing to different action methods:

... in Global.asax

routes.MapRoute( // this route must be declared first, before the one below it
     "StartBrowse",
     "Gallery/Browse/Start/Here",
     new
     {
         controller = "Gallery",
         action = "StartBrowse",
     });

routes.MapRoute(
     "ActualBrowse",
     "Gallery/Browse/{searchterm}",
     new
     {
         controller = "Gallery",
         action = "Browse",
         searchterm = UrlParameter.Optional
     });

... and in the controller...

public ActionResult Browse(string id)
{
    var summaries = /* search using id as search term */
    return View(summaries);
}

public ActionResult StartBrowse()
{
    var summaries = /* default list when nothing entered */
    return View(summaries);
}

You might also be able to keep the action methods named the same in the controller, by applying an [ActionName] attribute to one to distinguish it. Using the same Global.asax as above, your controller would then look like this:

public ActionResult Browse(string id)
{
    var summaries = /* search using id as search term */
    return View(summaries);
}

[ActionName("StartBrowse")]
public ActionResult Browse()
{
    var summaries = /* default list when nothing entered */
    return View(summaries);
}

这篇关于路线:采取行动的当前请求[...]是下面的操作方法之间的暧昧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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