C# 路由:当前的操作请求 [...] 在以下操作方法之间是不明确的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10668105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Routing: The current request for action [...] is ambiguous between the following action methods
提问by Dave
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.
我有一个名为 的视图Browse.chtml,用户可以在其中输入搜索词,或将搜索词留空。输入搜索词时,我想将页面定向到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. I'm not sure what else to try at this point.
我在 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);
}
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
});
采纳答案by danludwig
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. It will try to match a route to the method (or vice versa), and this algoritm is not strongly-typed.
尽管您的 2 个 Browse 方法是有效的 C# 重载,但 MVC 操作方法选择器无法确定要调用哪个方法。它将尝试将路由与方法匹配(反之亦然),并且此算法不是强类型的。
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. Using the same Global.asax as above, your controller would then look like this:
您也可以通过将属性应用于一个来区分它,从而在控制器中保持相同的操作方法命名[ActionName]。使用与上面相同的 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);
}
回答by Sayg?n Do?u
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);
}
回答by Darren Street
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 完成了这项工作。
回答by omar mohameed
Add following code in RouteConfig.cs before Defaultroute
在 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>");
}

