简体   繁体   English

路由:当前的动作请求 […] 在以下动作方法之间是不明确的

[英]Routing: The current request for action […] is ambiguous between the following action methods

I have a View called Browse.chtml , where the user can enter a search term, or leave the search term blank.我有一个名为Browse.chtml的视图,用户可以在其中输入搜索词,或将搜索词留空。 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 .输入搜索词时,我想将页面定向到http://localhost:62019/Gallery/Browse/{Searchterm} ,当没有输入时,我想将浏览器定向到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当前对控制器类型“GalleryController”的“浏览”操作请求在以下操作方法之间存在歧义: System.Web.Mvc.ActionResult Browse(System.String) on type AutoApp_MVC.Controllers.GalleryController System.Web.Mvc.ActionResult Browse (Int32, System.String) 类型为 AutoApp_MVC.Controllers.GalleryController

Everything I'm doing with MVC is for the first time.我在 MVC 上所做的一切都是第一次。 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:我在 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] .一个控制器上最多只能有 2 个同名的操作方法,为此,1 个必须是[HttpPost] ,另一个必须是[HttpGet]

Since both of your methods are GET, you should either rename one of the action methods or move it to a different controller.由于您的两个方法都是 GET,您应该重命名其中一个操作方法或将其移动到不同的控制器。

Though your 2 Browse methods are valid C# overloads, the MVC action method selector can't figure out which method to invoke.尽管您的 2 个 Browse 方法是有效的 C# 重载,但 MVC 操作方法选择器无法确定要调用哪个方法。 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 ... 在 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.您还可以通过将[ActionName]属性应用于一个来区分它,从而使控制器中的操作方法名称保持相同 Using the same Global.asax as above, your controller would then look like this:使用与上面相同的 Global.asax,您的控制器将如下所示:

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

I don't know when the question was asked this solution was available but you can use:我不知道这个问题何时被问到这个解决方案可用,但您可以使用:

Request.QueryString["key"]

So this should work fine for your problem:所以这应该可以很好地解决您的问题:

[HttpGet]
public ActionResult Browse()
{
    if( Request.QueryString["id"] != null )        
        var summaries = /* search using id as search term */
    else /*assuming you don't have any more option*/
        var summaries = /* default list when nothing entered */

    return View(summaries);
} 

Add following code in RouteConfig.cs before Default route在 RouteConfig.cs 中默认路由前添加以下代码

routes.MapMvcAttributeRoutes();

And add route attributes in the controller like:并在控制器中添加路由属性,如:

    [Route("Cars/deteals/{id:int}")]
    public ContentResult deteals(int id)
    {
        return Content("<b>Cars ID Is " + id + "</b>");
    }

    [Route("Cars/deteals/{name}")]
    public  ContentResult deteals(string name)
    {
        return Content("<b>Car name Is " + name + "</b>");

    }

I think the point being made is that you don't need to implicitly test for querystring parameters using the request class.我认为重点是您不需要使用请求类隐式测试查询字符串参数。

MVC does the mapping for you (unless you have made severe changes in your MVC routes). MVC 为您进行映射(除非您对 MVC 路由进行了重大更改)。

Thus an actionlink path of因此一个动作链接路径

/umbraco/Surface/LoginSurface/Logout?DestinationUrl=/home/

would automatically be available to your (surface) controller with the parameter defined:将自动可用于您的(表面)控制器并定义参数:

public ActionResult Logout(string DestinationUrl)

MVC does the work. MVC 完成了这项工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在以下操作方法之间,当前对控制器类型“ ...”的操作请求[…]不明确 - The current request for action […] on controller type '…' is ambiguous between the following action methods 在以下操作方法之间,当前对控制器类型“ AdminController”的操作“ AdminManagement”的请求不明确 - The current request for action 'AdminManagement' on controller type 'AdminController' is ambiguous between the following action methods 控制器类型&#39;WeeklyTargetController&#39;上的当前动作&#39;Create&#39;请求在以下操作方法之间是不明确的: - The current request for action 'Create' on controller type 'WeeklyTargetController' is ambiguous between the following action methods: MVC异常:以下操作方法之间存在歧义 - MVC Exception: Ambiguous between the following action methods 在以下操作方法之间请求歧义 - Request ambiguos between the following action methods Create 控制器类型{1}上的当前操作请求{0}不明确 - The current request for action {0} on controller type {1} is ambiguous AmbiguousMatchException:行动要求不明确 - AmbiguousMatchException: Request for action is ambiguous 控制器类型为{x}的当前操作请求{x}模棱两可 - The current request for action {x} on controller type {x} is ambiguous 具有不同HttpMethod的歧义操作方法 - Ambiguous action methods with different HttpMethod 动作路由:应用程序在GET和POST方法之间感到困惑 - Action Routing: Application gets confused between the GET and POST methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM