2

I am using asp.net MVC 4. Is there a way to use two http get methods on an action with the same name but different parameters? Or just a way to have the page be an index page for both so the name of the page is consistent? Also, int cannot be null.

Example:

Used for querystring and searches

   [HttpGet]
    public ActionResult Index(int num, string aString)
   {
   } 

Used for just an action link click(a default search)

 [HttpGet]
  public ActionResult Index()
 {
 } 
2
  • Why would you even need this kind of scenario? If it was any use, I guess MS would provide some kind of capacity to run it this way. Commented Mar 8, 2013 at 19:23
  • 1
    yeah its uncommon situation but that's per requirements, I think its not best also, maybe I could change the teams mind. good point. Commented Mar 9, 2013 at 13:13

3 Answers 3

3

No work-around. What you can do is following:

[HttpGet]
public ActionResult Index(int num, string aString)
{
      //if num == null, throw exception or do something
      // if string == null, do something, if not, do something else
} 
Sign up to request clarification or add additional context in comments.

7 Comments

It smiles like hack ... What would be the use case for this? To get model in get?
What if I need the int to not be null, but nothing is pass when you click a link.
@Ray: It's default behaviour. All 3 parameters are optional, then you check which once are missing and decide what to do.
@Peter: yes, model is weird but possible, but general behavior is valid. You can have 2-3 optional parameters in query string: '/action?num=1', '/action?astring=name' or '/action?num=1&astring=name'. This is all usual web use case.
@Ray: you can make private method with overloads and decide which overload you wanna call.
|
0
 - (Html . BeginForm ( "ViewName", "ControllerName", FormMethod.Get ) {
   Html . Hidden ( "actionname1", "true" ) form elements set 1 submit
   button 1 }

   (Html . BeginForm ( "ViewName", "ControllerName", FormMethod.Get ) {
   Html . Hidden("actionname2", "true") form elements set 2 submit
   button 2 }

   In Controller, check for "true "values for actionname1, actionname2
   and decide on control flow for each. Assumption: Same View and Same
   Controller WITH Two distinct FormMethod.Gets

   ..Paurav

Comments

0

What I actually end up doing was passing in an object with nullable values in one get http request action. As @Display Name suggested above in the comment Microsoft has this functionality built into the .net framework.

Comments

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.