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

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

当我尝试这样做时,我得到了错误:

当前对控制器类型“ GalleryController”的操作请求“ Browse”在以下操作方法之间是不明确的: 类型 AutoApp _ MVC. Controllers. GalleryController 上的 System.Web.Mvc.ActionResult 浏览(System.String) 类型 AutoApp _ MVC. Controllers. GalleryController 上的 System.Web.Mvc.ActionResult 浏览器(Int32,System.String)

我对 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
});
85146 次浏览

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

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

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).

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.

Add following code in RouteConfig.cs before Default route

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


}