路由:当前的动作请求 [...] 在以下动作方法之间是不明确的 [英] Routing: The current request for action [...] is ambiguous between the following action methods
问题描述
我有一个名为 Browse.chtml
的视图,用户可以在其中输入搜索词,或将搜索词留空.输入搜索词时,我想将页面定向到 http://localhost:62019/Gallery/Browse/{Searchterm}
而当没有输入时,我想将浏览器定向到 http://localhost:62019/Gallery/Browse/Start/Here
.
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:
当前对控制器类型GalleryController"的浏览"操作请求在以下操作方法之间不明确:System.Web.Mvc.ActionResult Browse(System.String) on type AutoApp_MVC.Controllers.GalleryControllerSystem.Web.Mvc.ActionResult Browse(Int32, System.String) on type AutoApp_MVC.Controllers.GalleryController
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 所做的一切都是第一次.我不知道现在还可以尝试什么.
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);
}
我在 Global.asax.cs 中也有这个:
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
});
推荐答案
一个控制器上最多只能有 2 个同名的 action 方法,为了做到这一点,1 个必须是 [HttpPost]
,另一个必须是[HttpGet]
.
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]
.
由于您的两个方法都是 GET,您应该重命名其中一个操作方法或将其移至不同的控制器.
Since both of your methods are GET, you should either rename one of the action methods or move it to a different controller.
尽管您的 2 个 Browse 方法是有效的 C# 重载,但 MVC 操作方法选择器无法确定要调用哪个方法.它将尝试将路由与方法匹配(反之亦然),并且此算法不是强类型的.
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:
... 在 Global.asax 中
... 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);
}
您也许还可以保留名为在控制器中相同,通过应用一个 [ActionName]
属性来区分它.使用与上面相同的 Global.asax,您的控制器将如下所示:
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屋!