views:

99

answers:

2

The routing I need is quite simple, I must be missing something there. As code example I put the simpler situation where I can reproduce my behavior.

You have this ActionMethod :

 public ActionResult Index(string provider)
 {
     ViewData["Message"] = provider;
     return View("Index");
 }

And you have this route :

 routes.MapRoute(
      null,
      "{controller}/{action}/{provider}", 
      new { controller = "Home", action = "Index", provider = "Default" }
 ); // Parameter defaults

You can call /Home/Index/Custom and provider will take the value "Custom"

What Route would I need if I want the url /?provider=Custom to map the provider to the parameter. I thought that would just work, because the default controller and the default action would be used, and the provider from the querystring would be used instead of the default one. but the querystring is just ignored here.

That's a problem in my situation as I have a form using HTTP GET method. The form action has to be Html.BeginForm(c=>c.Index(null)) which is resolved as / and the value of my form are added in the querystring. (the provider being a dropdown in the form)

So the url built by the form is /?abc=value&cde=value...

UPDATE

The accepted answer below (see the comments) led me to this solution:

 routes.MapRoute(
     "Search",
     "search/",
     new { controller = "Home", action = "Index" }
 );

 routes.MapRoute(
    null,
    "{controller}/{action}/{provider}",
    new { controller = "Home", action = "Index", provider = "Default"} 
 );

And declare the form like so :

 Html.BeginRouteForm("Search", FormMethod.Get){
 ...
 }

This way, the form will work with the provider in the QueryString (when I use the named route search) but in all other case, I will use the default route. :)

+1  A: 

When I set the provider to urlparameter.optional instead of a static value, I get the behavior that you are looking for. I don't think I can explain fully why this works whereas having a static default value set does not, but give it a try and see if it helps. If it works, you may also want to develop a custom route for your form so you can maintain your default provider in your routes as opposed to doing custom checking in your controllers.

routes.MapRoute( _
    "Default", _
    "{controller}/{action}/{provider}", _
    New With {.controller = "Home", .action = "Index", .provider = UrlParameter.Optional} _
)

UPDATE:

Also, you do not have to have the parameters in your route to pass them to a controller action method. For instance, using the route above, I can have this URL

http://localhost:49705/home/about/default?otherValue=testme

And this controller method

    Function About(ByVal provider As String, ByVal otherValue As String) As ActionResult
    ViewData("Message") = provider & "|" & otherValue
    Return View()
End Function

Which outputs the string default|testme

This URL does the same as above: http://localhost:49705/home/about/?provider=default&otherValue=testme

Tommy
Yes you're right, that works, except that I really need a default value there... I don't want to code the fallback to the default provider in every action which uses it.
Stephane
Tommy
It's a search page, You can search in different provider that you set in the form. But I also need to link from another page, directly to the search with correct provider.
Stephane
If that is the case, I would make a custom route for your search page. That would take care of all of your issues and allow you to link, etc.
Tommy
By custom for the search page you mean that the form's action should be a totally different URL? Like /search/provider?other=value... Yeah that should work! :) will try it tomorow
Stephane
It works like a charm. I'll set you as Valid answer and edit my qyestion to give the exact routes to set. thanks :)
Stephane
A: 

Maybe I'm not understanding the question, but if you just remove the {provider} from your route, or use the default {id} instead. Then when you set the URL to /?provider=blah, "blah" is assigned to the "provider" parameter.

dave thieben
Yes, but then I don't get a default value for my provider...And I don't get the nice url either when I have a link on my page. It would be fine if I didn't have a form that that use GET to get to that page. but I need both routed (for links) and QueryString (for the form)
Stephane