0

Is it possible to overload the action methods based on number of parameters in request?

Eg:

1. domain.com/List/Filter/ByName invokes -> public ActionResult Filter(string criteria1)

2. domain.com/List/Filter/ByName/ByRanking invokes -> public ActionResult Filter(string criteria1, string criteria2)

I'm using asp.net mvc2.

1
  • Unsure but in your ActionResult method, you can programmatically call other ActionResult methods. I do a lot of this. Commented Mar 6, 2011 at 8:56

2 Answers 2

3

Action methods cannot be overloaded based on parameters because there would be no reasonable way to disambiguate a URL into multiple overloaded methods.

What you can do, though is either this:

public ActionResult Filter(string criteria1, string criteria2)

and then check whether criteria2 is null to filter only by name.

Alternatively, you can use ActionNameAttribute to decorate your action methods

[ActionName("FilterByName")]
public ActionResult Filter(string criteria1)

[ActionName("FilterByNameAndRanking")]
public ActionResult Filter(string criteria1, string criteria2)

and then use that name in route registration. This approach, however, can lead to much confusion.

Sign up to request clarification or add additional context in comments.

1 Comment

How different is this from having a separate action name ie: isnt the above same as having 2 different action methods like public ActionResult FilterByName(string criteria1) public ActionResult FilterByNameAndRanking(string criteria1, string criteria2)
1

If I'm not mistaken the best way to do this would be to add two different controller methods and map them to two different Urls.

public ActionResult Filter1(string criteria1);

public ActionResult Filter2(string criteria1, criteria2);

Then you have two route definitions:

This will map this URL List/Filter/xxCriteria/ to the first controller

routes.MapRoute(
            "Filter",                                              // Route name
            "{controller}/Filter/{criteria1}",                           // URL with parameters
            new { controller = "List", action = "Filter1", criteria="" }  // Parameter defaults
        );

This will map this URL List/Filter/xxCriteriaName/xxxCriteriaRank to the second controller. Without this route you could still map a url to the second method, but it would look like : List/Filter/?criteria1=xx&criteria2=xx

routes.MapRoute(
            "Filter2",                                              // Route name
            "{controller}/Filter/{criteria1}/{criteria2}",                           // URL with parameters
            new { controller = "List", action = "Filter2", criteria1 = "", criteria2 = "" }  // Parameter defaults
        );

Hope it helped.

6 Comments

Whoops : Hey I didn't mean to use overloads, but two different filter methods. Edited the question! =P
I get resource not found in one of the url, but the second url works fine. What could be the problem?
@maxxxee ah, its just the url should be {criteria1} I forgot the 1 See my edit history. Changed it in the answer.
context.MapRoute( "filetr_c1", "Admin/{controller}/Filter/{searchby1}", new { controller = "List", action = "GetByCriteria2", searchby1 = "" } ); context.MapRoute( "filer_c2", "Admin/{controller}/Filter/{searchby1}/{searchby2}", new { controller = "List", action = "GetByCriteria2", searchby1 = "", searchby2 = "" } );
my route code is given above, it does not work. The first route gives "resource cannot be found" error. second works ok. sample urls: url with these pattern does not work localhost:52247/Admin/List/Filer/3-TestUser
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.