Routing: the Current Request for Action [...] Is Ambiguous Between the Following Action Methods

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

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

ambiguous between the following action methods

Look at the resource below hope it helps, Also i think you should start looking at Entity Framework for your Data Access.

You can do something like in this example

// GET: /Movies/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}

// POST: /Movies/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}

https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/examining-the-details-and-delete-methods

The current request for action [...] on controller type '...' is ambiguous between the following action methods

This is because you have two overloaded actions which result in the confusion of the routing. In your example, you thought this

 <a href='@Url.Action("ListProducts", "Product", new { id = 1, tull = "" })'>sub category</a>

would go to the ListProducts(int id , string something). No! that is wrong assumption....the url would be like yourdomain/1 because something is empty....which seem the first action.

So when something variable is empty, the two action methods are confusion for the routing engine.

So change the names to be different

public ActionResult ListProductsbyId(int? id)
{
var db = new DBProduct();
List<Product> listOfProducts;

listOfProducts = db.getAll(id);

return View(listOfProducts);
}
public ActionResult ListProductsByIdAndTull (int id, string tull)
{
var db = new DBProduct();
List<Product> listOfProducts;

listOfProducts = db.getAll(id,tull);

return View(listOfProducts);
}

or just one action

  public ActionResult ListProducts(int? id, string tull)
{
var db = new DBProduct();
List<Product> listOfProducts;
if(String.IsNullOrEmpty(tull)
{
listOfProducts = db.getAll(id);
}
else
{
listOfProducts = db.getAll(id,tull);
}

return View(listOfProducts);
}

MVC5 ambiguous action methods in controller

This isn't possible since you're trying to do a GET request for each method. The ASP.NET MVC action selector doesn't know which method to pick.

If it makes sense and you're able you can use the HttpGet or HttpPost attributes to differentiate the method for each type of HTTP request. It doesn't sound like that makes sense in your case.

Ambiguous action methods

You can't have the same HTTP verb for the same action name. In other words, having HttpGet for the same action name, even an overload, isn't possible.

You have three options:

Change one or your action methods to a different HTTP action verb...

[HttpGet]
public ActionResult Index()
{
//..
}

[HttpPost]
public ActionResult Index(string country)
{
//..
}

Or, change the name of your action method...

public ActionResult CountryIndex(string country)
{
ViewBag.Message = "my country=" + country;

return View();
}

Or, you can change the action name from the overloaded method...

public ActionResult Index()
{
//..
}

[ActionName("IndexByCountryName")]
public ActionResult Index(string country)
{
//..
}

Working example

This uses the last option, keeping the method name overloaded but specify the ActionNameAttribute for the overload

Actions

    public ActionResult Index()
{
ViewBag.Message = "no country selected!";

return View();
}

[ActionName("IndexByCountry")]
public ActionResult Index(string country)
{
ViewBag.Message = string.Format("County selected :: {0}", country);

// the below ActionResult reuses the Index view but
// here you could have a separate view for the country
// selection if you like
//
return View("Index");
}

Routes

        routes.MapRoute(
name: "ByCountry",
url: "{country}",
defaults: new { controller = "Home", action = "IndexByCountry" }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

The current request for action 'Index' on controller type 'DinnersController' is ambiguous between the following action methods

The error you are getting tells that ASP.NET MVC has found two actions with the same name and can't chose which to use.

If you have two Index-actions in your DinnersController.cs:

public ActionResult Index() {

and:

public ActionResult Index(int? page) {

Then you should remove the first Action. since the second is the 'updated' version of the first.
The second Action accepts requests to:

/Dinners

/Dinners?page=2

/Dinners/Index

/Dinners/Index?page=2

And with the change in RegisterRoutes it also accepts requests to:

/Dinners/Page/2

You can have two Actions with the same name, providing one is for saving (postbacks), where you decorate the saving action with [AcceptVerbs(HttpVerbs.Post)] or just [HttpPost]

MVC Exception: Ambiguous between the following action methods

So you clicked the SignIn button which is routed to the Login action. The error is caused because HttpParamActionAttribute is returning true for IsValidName for both SignIn and Login actions.

Your HttpParamActionAttribute returns true for IsValidName against the Login action because the Login action matches by name.

Now your other HttpParamActionAttribute on SignIn also returns true because request["SignIn"] is not equal to null.

Change your view to look for an action that is not "LogIn" and also not "SignIn". That way only the action that matches the button name will return true for IsValidName.



Related Topics



Leave a reply



Submit