c# - 路由 : The current request for action [. ..] 在以下操作方法之间不明确

标签 c# asp.net-mvc

我有一个名为 Browse.chtml 的 View ,用户可以在其中输入搜索词,或将搜索词留空。输入搜索词时,我想将页面定向到 http://localhost:62019/Gallery/Browse/{Searchterm} 当没有输入任何内容时,我想将浏览器定向到 http://localhost:62019/Gallery/Browse/Start/Here

当我尝试这样做时,出现错误:

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

我使用 MVC 所做的一切都是第一次。我不确定此时还可以尝试什么。

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

我在 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
         });

最佳答案

Controller 上最多只能有 2 个同名的操作方法,为此,1 个必须是 [HttpPost],另一个必须是 [HttpGet].

由于您的两个方法都是 GET,您应该重命名其中一个操作方法或将其移动到不同的 Controller 。

虽然您的 2 个浏览方法是有效的 C# 重载,但 MVC 操作方法选择器无法确定调用哪个方法。它将尝试将路由与方法匹配(反之亦然),并且该算法不是强类型的。

您可以使用指向不同操作方法的自定义路由来完成您想要的:

...在 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
     });

...并在 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);
}

您也可以 keep the action methods named the same in the controller , 通过将 [ActionName] 属性应用于一个来区分它。使用与上面相同的 Global.asax,您的 Controller 将如下所示:

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

关于c# - 路由 : The current request for action [. ..] 在以下操作方法之间不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668105/

相关文章:

c# - Linq数据类型不匹配问题

c# - 为什么我的数据表行中的值没有更新?

c# - TFS 构建服务器在警告 "Cannot find the last label"后卡住

c# - 如何在我的应用程序中找到 StackOverflowException 的来源

asp.net-mvc - 使用字符串常量键来避免魔法字符串键是个好主意吗?

asp.net - 如何在 ASP.NET MVC 中保留/保护编辑中的某些字段

asp.net-mvc - 一页中多个ajax beginform

javascript - WebBrowser 中具有 Doctype 的不同 JavaScript 行为

c# - Html.ActionLink() 不适用于传递 C# 对象

c# - 通过Web.Config参数动态生成 View