.NET MVC custom routing with empty parameters

Posted by user135498 on Stack Overflow See other posts from Stack Overflow or by user135498
Published on 2009-10-28T22:50:39Z Indexed on 2010/04/02 2:03 UTC
Read the original article Hit count: 317

Filed under:
|
|
|

Hi All,

I have a .net mvc with the following routes:

routes.Add(new Route(
            "Lookups/{searchtype}/{inputtype}/{firstname}/{middlename}/{lastname}/{city}/{state}/{address}", 
            new RouteValueDictionary( new { controller = "Lookups", action = "Search", firstname = (string)null, middlename = (string)null, lastname = (string)null, city = (string)null, state = (string)null, address = (string)null, SearchType = SearchType.PeopleSearch, InputType = InputType.Name }),
            new MvcRouteHandler())
        );

        routes.Add(new Route(
            "Lookups/{searchtype}/{inputtype}", 
            new RouteValueDictionary( new { controller = "Lookups", action = "Search", firstname = "", middlename = "", lastname = "", city = "", state = "", address = "" }),
            new MvcRouteHandler())
        );

        routes.Add(new Route(
            "Lookups/{searchtype}/{inputtype}", 
            new RouteValueDictionary( new { controller = "Lookups", action = "Search", firstname = "", middlename = "", lastname = "", city = "", state = "", address = "", SearchType = SearchType.PeopleSearch, InputType = InputType.Name }),
            new MvcRouteHandler())
        );

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Account", action = "LogOn", id = "" }  // Parameter defaults
        );

The following request works fine:

http://localhost:2608/Lookups/PeopleSearch/Name/john/w/smith/seattle/wa/123 main

This request does not work:

http://localhost:2608/Lookups/PeopleSearch/Name/john//smith//wa/

Not all requests will have all paramters and I would like empty parameters to be passed to the method as empty string or null.

Where am I going wrong?

The method:

public ActionResult Search(string firstname, string middlename, string lastname, string city, string state, string address, SearchType searchtype, InputType inputtype)
    {
        SearchRequest r = new SearchRequest { Firstname = firstname, Middlename = middlename, Lastname = lastname, City = city, State = state, Address = address, SearchType = searchtype, InputType = inputtype };
        return View(r);
    }

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about routing