views:

43

answers:

1

Hello.

How should I define route in my global.asax to be able use nullable parameters and coma as separator?

I'm trying to implement routing rule for my search users page like

"{Controller}/{Action},{name},{page},{status}"

Full entry from the Global.asax:

    routes.MapRoute(
        "Search",
        "{controller}/{action},{name},{page},{status}",
            new { controller = "User", action = "Find", 
                name = UrlParameter.Optional,
                page = UrlParameter.Optional,
                status = UrlParameter.Optional  } 
    );

Routine defined like above works fine when I'm entering all parameters, but when some parameters are equal to null routing fails (for example "user/find,,,")


According to Clicktricity comment bellow - the singature of action method that handes the request:

public ActionResult Find(string userName, int? page, int? status)
{
    // [...] some actions to handle the request

}

On the beginning I was testing the route by VS debugger, now I'm using route debugger described on Phil's Haack blog. The tool confirm - that routing with null values is not properly handled (or I'm doing something wrong ;) )

+1  A: 

As far as I know .Net routing doesn't let you do multiple nullable parameters like that. Multiple parameters will only work if they are missing working backwards from the end and with the separator also missing so you'd get matches on

user/find,bob,2,live
user/find,bob,2
user/find,bob
user/find

It'd be a lot easier to use querystrings for what you're trying to do.

Edit based on comment:

If this is necessary then you could try doing it this way (though it's not a nice approach)

Change your path to match

{Controller}/{Action},{*parameters}

Make sure to put a constraint on the action and controller so this is limited to as few as possible.

Rename each action that would take your full list to something else, adding a standard prefix to each one would be the cleanest way, and add the [NonAction] attribute. Add a new method with the original name that takes a string, this string is a comma separated string of your variables. In this method split the string and return the original action passing in the values from the split.

So you go from:

public ActionResult Find(string name, int page, string status){
    //Do stuff here
    return View(result);
}

To

public ActionResult Find(string parameters){
    string name;
    int? page;
    string status;
    //split parameters and parse into variables
    return FindAction(name, page, status);
}

[NonAction]
public ActionResult FindAction(string parameters){
    //Do what you did in your previous Find action
    return View(results);
}
Chao
Thank you for response. I was expecting that it's not possible directly. I know that I can get param values from query string, but my customer requirement is to pass data as shown above.
yaki
See the updated response.
Chao
Thanx Chao. I figured out something similar but my way was ugly comparing to your version. Thanx again ;)
yaki