开发者

inbound/outbound url routing in asp.net MVC

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 fo开发者_运维问答rm 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. :)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜