Question

I have a problem with a sample routing with the preview 5 of asp.net mvc.

In the AccountController I have 2 actions:

public ActionResult Delete()  
public ActionResult Delete(string username)

While trying to look for Account/Delete or Account/Delete?username=davide the ControllerActionInvoker throws a exception saying that Delete request is ambiguous between my tow actions methods.

The default route in the global.asax hasn't been changed.

Shouldn't the action invoker understand what's the method to call looking in the parameters list?

Using the preview 4 I hadn't these kind of problem performing the same operation.

Any idea?

Was it helpful?

Solution

Solution found!

With the introduction of the ActionNameAttribute, it's now necessary to filter manually which method to call depending on the request. This is done by the ActionSelectionAttribute.

Full explanation here: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

OTHER TIPS

I can't say for sure why this is happening. But you might want to consider only having the Delete(string username) action and removing the parameter-less overload.

Because string is nullable my understanding is that simply calling Account/Delete will invoke the action with a null username parameter which you can then test for at the beginning of the action method.

What I'd do is ditch the blank Delete(), and only use Delete(string username)

In your url routing you'd have something similar to "/{Controller}/{Action}/{username}/" ? If you have "/{Controller}/{Action}/{Id}/" you'd be better off doing Delete(string id) and that way just using the url to handle this "/Account/Delete/davide/"

That said use your default route which should be something like the default Id is ""

Then in your Delete(string id) method have:

public ActionResult Delete(string id)
{
    if(string.IsNullOrEmpty(id)) return EmptyID();

    // Continue normal Delete method
}

public ActionResult EmptyID()
{
    // The method you were going to have on a blank delete.
}

That or just wrap it up in the one method on an if {} else {}

Either way I'd just be going with the one method and doing a default on your username/id in your route of an empty string and handle it that way.

If you want to contact me on further follow up to what I mean, or whatever will help, ping me at andrew@ my domain on my info page.

Edit: Ah pretty much what Berko said anyway, I'm not sure how Named Attributes would help - so please post a comment here detailing it for other guys who find the same issues! :)

Its ambiguous because the two controller action are the same post method.. You can only used that in form posting scenario for example you are submitting a form data that uses HTTP post..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top