0

I have an typical MVC application that has the following Action:

public ActionResult MyAction(string param1)

When I call this action this way:

http://domain/MyController/MyAction?param1=dasas

I receive param1 as supposed: "dasas"

But in MVC the idea is to pass params as following right?

http://domain/MyController/MyAction/dasas

This way it doesn't work and I am wondering why! Any idea?

UPDATE: Here is my route:

routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: 
                new { 
                    controller = MVC.RegistrationAuthority.Name,
                    action = MVC.RegistrationAuthority.ActionNames.Landing, 
                    id = UrlParameter.Optional 
                });
Bonomi
  • 2,541
  • 5
  • 39
  • 51
  • On "But in MVC the idea is to pass params as following" - not necessary - it is up to you to decide what should be part of url and what should be passed as query parameters. – Alexei Levenkov Oct 02 '14 at 15:59
  • 1
    I think it is already answered in linked duplicate. When following suggestion in duplicate (same as Anthony Shaw's answer) make sure to properly order routes - more specific routes should go *before* more generic once like default (i.e. "/foo" before "/{controller}"). – Alexei Levenkov Oct 02 '14 at 16:00

1 Answers1

1

You need to configure your route to know that he param being passed in is called param1

Out of the box asp.net mvc only figured for an {id} param

routes.MapRoute(null, "MyController/MyAction/{param1}", new { controller = "MyController", action = "MyAction"});

be sure to add this route before other routes that could potentially override it (i.e. the default route)

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
  • Is it not supposed to work with the default route (I updated the question)? I tried do add your code but it continues to come null – Bonomi Oct 02 '14 at 15:56
  • I changed the sequence of the rules, putting the more generic one in the end and it works. Anthony, your answer helped me. – Bonomi Oct 02 '14 at 16:15
  • yes, sorry. I should have been specific, your most generic route needs to come first – Anthony Shaw Oct 02 '14 at 17:24